1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\" $Id: socket.2,v 1.4 1999/05/13 11:33:42 freitag Exp $
8 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
9 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
10 .\" Modified 1998, 1999 by Andi Kleen <ak@muc.de>
11 .\" Modified 2002-07-17 by Michael Kerrisk <mtk.manpages@gmail.com>
12 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
14 .TH SOCKET 2 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
16 socket \- create an endpoint for communication
19 .RI ( libc ", " \-lc )
22 .B #include <sys/socket.h>
24 .BI "int socket(int " domain ", int " type ", int " protocol );
28 creates an endpoint for communication and returns a file descriptor
29 that refers to that endpoint.
30 The file descriptor returned by a successful call will be
31 the lowest-numbered file descriptor not currently open for the process.
35 argument specifies a communication domain; this selects the protocol
36 family which will be used for communication.
37 These families are defined in
39 The formats currently understood by the Linux kernel include:
60 T}:IPv4 Internet protocols:T{
66 Amateur radio AX.25 protocol
68 .\" Part of ax25-tools
73 T}:IPX \- Novell protocols:
81 T}:ITU-T X.25 / ISO-8208 protocol:T{
86 T}:IPv6 Internet protocols:T{
92 DECet protocol sockets
97 Key management protocol, originally developed for usage with IPsec
102 Kernel user interface device
109 Low-level packet interface
116 .\" commit: 639b321b4d8f4e412bfbb2a4a19bfebc1e68ace4
117 Reliable Datagram Sockets (RDS) protocol
119 .\" rds-tools: https://github.com/oracle/rds-tools/blob/master/rds.7
120 .\" rds-tools: https://github.com/oracle/rds-tools/blob/master/rds-rdma.7
128 Generic PPP transport layer, for setting up L2 tunnels
134 .\" linux-history commit: 34beb106cde7da233d4df35dd3d6cf4fee937caa
135 Logical link control (IEEE 802.2 LLC) protocol
140 .\" commits: 8d36eb01da5d371f..ce117ffac2e93334
141 InfiniBand native addressing
146 .\" commits: 0189197f441602acdca3f97750d392a895b778fd
147 Multiprotocol Label Switching
152 .\" commits: 8dbde28d9711475a..5423dd67bd0108a1
153 Controller Area Network automotive bus protocol
158 .\" commits: b97bf3fd8f6a16966d4f18983b2c40993ff937d4
159 TIPC, "cluster domain sockets" protocol
164 .\" commits: 8d36eb01da5d371f..ce117ffac2e93334
165 Bluetooth low-level socket protocol
170 .\" commit: 03c8efc1ffeb6b82a22c1af8dd908af349563314
171 Interface to kernel crypto API
176 .\" commit: d021c344051af91f42c5ba9fdedc176740cbd238
177 VSOCK (originally "VMWare VSockets") protocol
178 for hypervisor-guest communication
185 .\" commit: 03c8efc1ffeb6b82a22c1af8dd908af349563314
186 KCM (kernel connection multiplexer) interface
191 .\" commit: c0c77d8fb787cfe0c3fca689c2a30d1dad4eaba7
192 XDP (express data path) interface
196 Further details of the above address families,
197 as well as information on several other address families, can be found in
198 .BR address_families (7).
200 The socket has the indicated
202 which specifies the communication semantics.
203 Currently defined types
207 Provides sequenced, reliable, two-way, connection-based byte streams.
208 An out-of-band data transmission mechanism may be supported.
211 Supports datagrams (connectionless, unreliable messages of a fixed
215 Provides a sequenced, reliable, two-way connection-based data
216 transmission path for datagrams of fixed maximum length; a consumer is
217 required to read an entire packet with each input system call.
220 Provides raw network protocol access.
223 Provides a reliable datagram layer that does not guarantee ordering.
226 Obsolete and should not be used in new programs;
230 Some socket types may not be implemented by all protocol families.
232 Since Linux 2.6.27, the
234 argument serves a second purpose:
235 in addition to specifying a socket type,
236 it may include the bitwise OR of any of the following values,
237 to modify the behavior of
243 file status flag on the open file description (see
245 referred to by the new file descriptor.
246 Using this flag saves extra calls to
248 to achieve the same result.
251 Set the close-on-exec
253 flag on the new file descriptor.
254 See the description of the
258 for reasons why this may be useful.
262 specifies a particular protocol to be used with the socket.
263 Normally only a single protocol exists to support a particular
264 socket type within a given protocol family, in which case
266 can be specified as 0.
267 However, it is possible that many protocols may exist, in
268 which case a particular protocol must be specified in this manner.
269 The protocol number to use is specific to the \*(lqcommunication domain\*(rq
270 in which communication is to take place; see
274 on how to map protocol name strings to protocol numbers.
278 are full-duplex byte streams.
281 A stream socket must be in
284 state before any data may be sent or received on it.
286 another socket is created with a
289 Once connected, data may be transferred using
293 calls or some variant of the
298 When a session has been completed a
301 Out-of-band data may also be transmitted as described in
303 and received as described in
306 The communications protocols which implement a
308 ensure that data is not lost or duplicated.
309 If a piece of data for which
310 the peer protocol has buffer space cannot be successfully transmitted
311 within a reasonable length of time, then the connection is considered
315 is enabled on the socket the protocol checks in a protocol-specific
316 manner if the other end is still alive.
319 signal is raised if a process sends or receives
320 on a broken stream; this causes naive processes,
321 which do not handle the signal, to exit.
323 sockets employ the same system calls as
326 The only difference is that
328 calls will return only the amount of data requested,
329 and any data remaining in the arriving packet will be discarded.
330 Also all message boundaries in incoming datagrams are preserved.
335 sockets allow sending of datagrams to correspondents named in
338 Datagrams are generally received with
340 which returns the next datagram along with the address of its sender.
343 is an obsolete socket type to receive raw packets directly from the
352 operation can be used to specify a process or process group to receive a
354 signal when the out-of-band data arrives or
358 connection breaks unexpectedly.
359 This operation may also be used to set the process or process group
360 that receives the I/O and asynchronous notification of I/O events via
372 When the network signals an error condition to the protocol module (e.g.,
373 using an ICMP message for IP) the pending error flag is set for the socket.
374 The next operation on this socket will return the error code of the pending
376 For some protocols it is possible to enable a per-socket error queue
377 to retrieve detailed information about the error; see
382 The operation of sockets is controlled by socket level
384 These options are defined in
390 are used to set and get options.
392 On success, a file descriptor for the new socket is returned.
393 On error, \-1 is returned, and
395 is set to indicate the error.
399 Permission to create a socket of the specified type and/or protocol
403 The implementation does not support the specified address family.
406 Unknown protocol, or protocol family not available.
409 .\" Since Linux 2.6.27
414 The per-process limit on the number of open file descriptors has been reached.
417 The system-wide limit on the total number of open files has been reached.
419 .BR ENOBUFS " or " ENOMEM
420 Insufficient memory is available.
422 created until sufficient resources are freed.
425 The protocol type or the specified protocol is not
426 supported within this domain.
428 Other errors may be generated by the underlying protocol modules.
430 POSIX.1-2001, POSIX.1-2008, 4.4BSD.
436 flags are Linux-specific.
440 It is generally portable to/from
441 non-BSD systems supporting clones of the BSD socket layer (including
444 The manifest constants used under 4.x BSD for protocol families
451 and so on are used for address
453 However, already the BSD man page promises: "The protocol
454 family generally is the same as the address family", and subsequent
455 standards use AF_* everywhere.
457 An example of the use of
480 .BR address_families (7),
487 \(lqAn Introductory 4.3BSD Interprocess Communication Tutorial\(rq
489 \(lqBSD Interprocess Communication Tutorial\(rq,
491 .I UNIX Programmer's Supplementary Documents Volume 1.