1socket(2) System Calls Manual socket(2)
2
3
4
6 socket - create an endpoint for communication
7
9 Standard C library (libc, -lc)
10
12 #include <sys/socket.h>
13
14 int socket(int domain, int type, int protocol);
15
17 socket() creates an endpoint for communication and returns a file de‐
18 scriptor that refers to that endpoint. The file descriptor returned by
19 a successful call will be the lowest-numbered file descriptor not cur‐
20 rently open for the process.
21
22 The domain argument specifies a communication domain; this selects the
23 protocol family which will be used for communication. These families
24 are defined in <sys/socket.h>. The formats currently understood by the
25 Linux kernel include:
26
27 Name Purpose Man page
28 AF_UNIX Local communication unix(7)
29 AF_LOCAL Synonym for AF_UNIX
30 AF_INET IPv4 Internet protocols ip(7)
31 AF_AX25 Amateur radio AX.25 protocol ax25(4)
32 AF_IPX IPX - Novell protocols
33 AF_APPLETALK AppleTalk ddp(7)
34 AF_X25 ITU-T X.25 / ISO-8208 protocol x25(7)
35 AF_INET6 IPv6 Internet protocols ipv6(7)
36 AF_DECnet DECet protocol sockets
37 AF_KEY Key management protocol, originally de‐
38 veloped for usage with IPsec
39 AF_NETLINK Kernel user interface device netlink(7)
40 AF_PACKET Low-level packet interface packet(7)
41 AF_RDS Reliable Datagram Sockets (RDS) protocol rds(7)
42 rds-rdma(7)
43 AF_PPPOX Generic PPP transport layer, for setting
44 up L2 tunnels (L2TP and PPPoE)
45 AF_LLC Logical link control (IEEE 802.2 LLC)
46 protocol
47 AF_IB InfiniBand native addressing
48 AF_MPLS Multiprotocol Label Switching
49 AF_CAN Controller Area Network automotive bus
50 protocol
51 AF_TIPC TIPC, "cluster domain sockets" protocol
52 AF_BLUETOOTH Bluetooth low-level socket protocol
53 AF_ALG Interface to kernel crypto API
54 AF_VSOCK VSOCK (originally "VMWare VSockets") vsock(7)
55 protocol for hypervisor-guest communica‐
56 tion
57 AF_KCM KCM (kernel connection multiplexer) in‐
58 terface
59 AF_XDP XDP (express data path) interface
60
61 Further details of the above address families, as well as information
62 on several other address families, can be found in address_families(7).
63
64 The socket has the indicated type, which specifies the communication
65 semantics. Currently defined types are:
66
67 SOCK_STREAM Provides sequenced, reliable, two-way, connection-based
68 byte streams. An out-of-band data transmission mecha‐
69 nism may be supported.
70
71 SOCK_DGRAM Supports datagrams (connectionless, unreliable messages
72 of a fixed maximum length).
73
74 SOCK_SEQPACKET Provides a sequenced, reliable, two-way connection-
75 based data transmission path for datagrams of fixed
76 maximum length; a consumer is required to read an en‐
77 tire packet with each input system call.
78
79 SOCK_RAW Provides raw network protocol access.
80
81 SOCK_RDM Provides a reliable datagram layer that does not guar‐
82 antee ordering.
83
84 SOCK_PACKET Obsolete and should not be used in new programs; see
85 packet(7).
86
87 Some socket types may not be implemented by all protocol families.
88
89 Since Linux 2.6.27, the type argument serves a second purpose: in addi‐
90 tion to specifying a socket type, it may include the bitwise OR of any
91 of the following values, to modify the behavior of socket():
92
93 SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the open file
94 description (see open(2)) referred to by the new file
95 descriptor. Using this flag saves extra calls to fc‐
96 ntl(2) to achieve the same result.
97
98 SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file
99 descriptor. See the description of the O_CLOEXEC flag
100 in open(2) for reasons why this may be useful.
101
102 The protocol specifies a particular protocol to be used with the
103 socket. Normally only a single protocol exists to support a particular
104 socket type within a given protocol family, in which case protocol can
105 be specified as 0. However, it is possible that many protocols may ex‐
106 ist, in which case a particular protocol must be specified in this man‐
107 ner. The protocol number to use is specific to the “communication do‐
108 main” in which communication is to take place; see protocols(5). See
109 getprotoent(3) on how to map protocol name strings to protocol numbers.
110
111 Sockets of type SOCK_STREAM are full-duplex byte streams. They do not
112 preserve record boundaries. A stream socket must be in a connected
113 state before any data may be sent or received on it. A connection to
114 another socket is created with a connect(2) call. Once connected, data
115 may be transferred using read(2) and write(2) calls or some variant of
116 the send(2) and recv(2) calls. When a session has been completed a
117 close(2) may be performed. Out-of-band data may also be transmitted as
118 described in send(2) and received as described in recv(2).
119
120 The communications protocols which implement a SOCK_STREAM ensure that
121 data is not lost or duplicated. If a piece of data for which the peer
122 protocol has buffer space cannot be successfully transmitted within a
123 reasonable length of time, then the connection is considered to be
124 dead. When SO_KEEPALIVE is enabled on the socket the protocol checks
125 in a protocol-specific manner if the other end is still alive. A SIG‐
126 PIPE signal is raised if a process sends or receives on a broken
127 stream; this causes naive processes, which do not handle the signal, to
128 exit. SOCK_SEQPACKET sockets employ the same system calls as
129 SOCK_STREAM sockets. The only difference is that read(2) calls will
130 return only the amount of data requested, and any data remaining in the
131 arriving packet will be discarded. Also all message boundaries in in‐
132 coming datagrams are preserved.
133
134 SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to corre‐
135 spondents named in sendto(2) calls. Datagrams are generally received
136 with recvfrom(2), which returns the next datagram along with the ad‐
137 dress of its sender.
138
139 SOCK_PACKET is an obsolete socket type to receive raw packets directly
140 from the device driver. Use packet(7) instead.
141
142 An fcntl(2) F_SETOWN operation can be used to specify a process or
143 process group to receive a SIGURG signal when the out-of-band data ar‐
144 rives or SIGPIPE signal when a SOCK_STREAM connection breaks unexpect‐
145 edly. This operation may also be used to set the process or process
146 group that receives the I/O and asynchronous notification of I/O events
147 via SIGIO. Using F_SETOWN is equivalent to an ioctl(2) call with the
148 FIOSETOWN or SIOCSPGRP argument.
149
150 When the network signals an error condition to the protocol module
151 (e.g., using an ICMP message for IP) the pending error flag is set for
152 the socket. The next operation on this socket will return the error
153 code of the pending error. For some protocols it is possible to enable
154 a per-socket error queue to retrieve detailed information about the er‐
155 ror; see IP_RECVERR in ip(7).
156
157 The operation of sockets is controlled by socket level options. These
158 options are defined in <sys/socket.h>. The functions setsockopt(2) and
159 getsockopt(2) are used to set and get options.
160
162 On success, a file descriptor for the new socket is returned. On er‐
163 ror, -1 is returned, and errno is set to indicate the error.
164
166 EACCES Permission to create a socket of the specified type and/or pro‐
167 tocol is denied.
168
169 EAFNOSUPPORT
170 The implementation does not support the specified address fam‐
171 ily.
172
173 EINVAL Unknown protocol, or protocol family not available.
174
175 EINVAL Invalid flags in type.
176
177 EMFILE The per-process limit on the number of open file descriptors has
178 been reached.
179
180 ENFILE The system-wide limit on the total number of open files has been
181 reached.
182
183 ENOBUFS or ENOMEM
184 Insufficient memory is available. The socket cannot be created
185 until sufficient resources are freed.
186
187 EPROTONOSUPPORT
188 The protocol type or the specified protocol is not supported
189 within this domain.
190
191 Other errors may be generated by the underlying protocol modules.
192
194 POSIX.1-2008.
195
196 SOCK_NONBLOCK and SOCK_CLOEXEC are Linux-specific.
197
199 POSIX.1-2001, 4.4BSD.
200
201 socket() appeared in 4.2BSD. It is generally portable to/from non-BSD
202 systems supporting clones of the BSD socket layer (including System V
203 variants).
204
205 The manifest constants used under 4.x BSD for protocol families are
206 PF_UNIX, PF_INET, and so on, while AF_UNIX, AF_INET, and so on are used
207 for address families. However, already the BSD man page promises: "The
208 protocol family generally is the same as the address family", and sub‐
209 sequent standards use AF_* everywhere.
210
212 An example of the use of socket() is shown in getaddrinfo(3).
213
215 accept(2), bind(2), close(2), connect(2), fcntl(2), getpeername(2),
216 getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2),
217 select(2), send(2), shutdown(2), socketpair(2), write(2), getpro‐
218 toent(3), address_families(7), ip(7), socket(7), tcp(7), udp(7),
219 unix(7)
220
221 “An Introductory 4.3BSD Interprocess Communication Tutorial” and “BSD
222 Interprocess Communication Tutorial”, reprinted in UNIX Programmer's
223 Supplementary Documents Volume 1.
224
225
226
227Linux man-pages 6.05 2023-03-30 socket(2)