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