1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="TCP/IP Library Reference"
23 HREF="tcpip-library-reference.html"><LINK
26 HREF="net-common-tcpip-manpages-shutdown.html"><LINK
29 HREF="net-common-tcpip-manpages-socketpair.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="net-common-tcpip-manpages-shutdown.html"
65 >Chapter 38. TCP/IP Library Reference</TD
71 HREF="net-common-tcpip-manpages-socketpair.html"
85 NAME="NET-COMMON-TCPIP-MANPAGES-SOCKET">socket</H1
94 >SOCKET(2) System Calls Manual SOCKET(2)
97 socket - create an endpoint for communication
100 #include <sys/types.h>
101 #include <sys/socket.h>
104 socket(int domain, int type, int protocol);
107 socket() creates an endpoint for communication and returns a descriptor.
109 The domain parameter specifies a communications domain within which com-
110 munication will take place; this selects the protocol family which should
111 be used. These families are defined in the include file <sys/socket.h>.
112 The currently understood formats are
114 AF_UNIX (UNIX internal protocols),
115 AF_INET (ARPA Internet protocols),
116 AF_INET6 (ARPA IPv6 protocols),
117 AF_ISO (ISO protocols),
118 AF_NS (Xerox Network Systems protocols),
119 AF_IPX (Internetwork Packet Exchange), and
120 AF_IMPLINK (IMP host at IMP link layer).
122 The socket has the indicated type, which specifies the semantics of com-
123 munication. Currently defined types are:
131 A SOCK_STREAM type provides sequenced, reliable, two-way connection based
132 byte streams. An out-of-band data transmission mechanism may be sup-
133 ported. A SOCK_DGRAM socket supports datagrams (connectionless, unreli-
134 able messages of a fixed (typically small) maximum length). A
135 SOCK_SEQPACKET socket may provide a sequenced, reliable, two-way connec-
136 tion-based data transmission path for datagrams of fixed maximum length;
137 a consumer may be required to read an entire packet with each read system
138 call. This facility is protocol specific, and presently implemented only
139 for PF_NS. SOCK_RAW sockets provide access to internal network protocols
140 and interfaces. The types SOCK_RAW, which is available only to the supe-
141 ruser, and SOCK_RDM, which is planned, but not yet implemented, are not
144 The protocol specifies a particular protocol to be used with the socket.
145 Normally only a single protocol exists to support a particular socket
146 type within a given protocol family. However, it is possible that many
147 protocols may exist, in which case a particular protocol must be speci-
148 fied in this manner. The protocol number to use is particular to the
149 communication domain in which communication is to take place; see
150 protocols(5). A value of 0 for protocol will let the system select an
151 appropriate protocol for the requested socket type.
153 Sockets of type SOCK_STREAM are full-duplex byte streams, similar to
154 pipes. A stream socket must be in a connected state before any data may
155 be sent or received on it. A connection to another socket is created
156 with a connect(2) call. Once connected, data may be transferred using
157 read(2) and write(2) calls or some variant of the send(2) and recv(2)
158 calls. When a session has been completed a close(2) may be performed.
159 Out-of-band data may also be transmitted as described in send(2) and
160 received as described in recv(2).
162 The communications protocols used to implement a SOCK_STREAM ensure that
163 data is not lost or duplicated. If a piece of data for which the peer
164 protocol has buffer space cannot be successfully transmitted within a
165 reasonable length of time, then the connection is considered broken and
166 calls will indicate an error with -1 returns and with ETIMEDOUT as the
167 specific code in the global variable errno. The protocols optionally
168 keep sockets ``warm'' by forcing transmissions roughly every minute in
169 the absence of other activity. An error is then indicated if no response
170 can be elicited on an otherwise idle connection for a extended period
171 (e.g., 5 minutes). A SIGPIPE signal is raised if a process sends on a
172 broken stream; this causes naive processes, which do not handle the sig-
175 SOCK_SEQPACKET sockets employ the same system calls as SOCK_STREAM sock-
176 ets. The only difference is that read(2) calls will return only the
177 amount of data requested, and any remaining in the arriving packet will
180 SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to correspon-
181 dents named in send(2) calls. Datagrams are generally received with
182 recvfrom(2), which returns the next datagram with its return address.
184 An fcntl(2) call can be used to specify a process group to receive a
185 SIGURG signal when the out-of-band data arrives. It may also enable non-
186 blocking I/O and asynchronous notification of I/O events via SIGIO.
188 The operation of sockets is controlled by socket level options. These
189 options are defined in the file <sys/socket.h>. setsockopt(2) and
190 getsockopt(2) are used to set and get options, respectively.
193 A -1 is returned if an error occurs, otherwise the return value is a
194 descriptor referencing the socket.
197 The socket() call fails if:
199 [EPROTONOSUPPORT] The protocol type or the specified protocol is not
200 supported within this domain.
202 [EMFILE] The per-process descriptor table is full.
204 [ENFILE] The system file table is full.
206 [EACCES] Permission to create a socket of the specified type
207 and/or protocol is denied.
209 [ENOBUFS] Insufficient buffer space is available. The socket
210 cannot be created until sufficient resources are
214 accept(2), bind(2), connect(2), getsockname(2), getsockopt(2), ioctl(2),
215 listen(2), poll(2), read(2), recv(2), select(2), send(2), setsockopt(2),
216 shutdown(2), socketpair(2), write(2), getprotoent(3), netintro(4)
218 An Introductory 4.3 BSD Interprocess Communication Tutorial, reprinted in
219 UNIX Programmer's Supplementary Documents Volume 1.
221 BSD Interprocess Communication Tutorial, reprinted in UNIX Programmer's
222 Supplementary Documents Volume 1.
225 The socket() function call appeared in 4.2BSD.
238 SUMMARY="Footer navigation table"
249 HREF="net-common-tcpip-manpages-shutdown.html"
267 HREF="net-common-tcpip-manpages-socketpair.html"
283 HREF="tcpip-library-reference.html"