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