]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man2/socket.2
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man2 / socket.2
CommitLineData
a1eaacb1 1'\" t
77117f4f
MK
2.\" Copyright (c) 1983, 1991 The Regents of the University of California.
3.\" All rights reserved.
4.\"
47009d5e 5.\" SPDX-License-Identifier: BSD-4-Clause-UC
77117f4f
MK
6.\"
7.\" $Id: socket.2,v 1.4 1999/05/13 11:33:42 freitag Exp $
8.\"
9.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
10.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
11.\" Modified 1998, 1999 by Andi Kleen <ak@muc.de>
12.\" Modified 2002-07-17 by Michael Kerrisk <mtk.manpages@gmail.com>
13.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
14.\"
4c1c5274 15.TH socket 2 (date) "Linux man-pages (unreleased)"
77117f4f
MK
16.SH NAME
17socket \- create an endpoint for communication
3b188e1a
AC
18.SH LIBRARY
19Standard C library
8fc3b2cf 20.RI ( libc ", " \-lc )
77117f4f 21.SH SYNOPSIS
c7db92b9 22.nf
77117f4f 23.B #include <sys/socket.h>
c6d039a3 24.P
77117f4f 25.BI "int socket(int " domain ", int " type ", int " protocol );
c7db92b9 26.fi
77117f4f
MK
27.SH DESCRIPTION
28.BR socket ()
29899888
MK
29creates an endpoint for communication and returns a file descriptor
30that refers to that endpoint.
047013ac
MK
31The file descriptor returned by a successful call will be
32the lowest-numbered file descriptor not currently open for the process.
c6d039a3 33.P
77117f4f
MK
34The
35.I domain
36argument specifies a communication domain; this selects the protocol
37family which will be used for communication.
38These families are defined in
39.IR <sys/socket.h> .
22570de1 40The formats currently understood by the Linux kernel include:
77117f4f
MK
41.TS
42tab(:);
698c7b2f 43l1 lw40 l.
77117f4f
MK
44Name:Purpose:Man page
45T{
1ae6b2c7 46.B AF_UNIX
77117f4f
MK
47T}:T{
48Local communication
49T}:T{
50.BR unix (7)
51T}
52T{
e900e16c
MK
53.B AF_LOCAL
54T}:T{
55Synonym for
56.B AF_UNIX
57T}:T{
58T}
59T{
d4c8c97c 60.B AF_INET
77117f4f
MK
61T}:IPv4 Internet protocols:T{
62.BR ip (7)
63T}
64T{
5880549a
ES
65.B AF_AX25
66T}:T{
67Amateur radio AX.25 protocol
68T}:T{
69.\" Part of ax25-tools
70.BR ax25 (4)
77117f4f
MK
71T}
72T{
d4c8c97c 73.B AF_IPX
77117f4f
MK
74T}:IPX \- Novell protocols:
75T{
5880549a
ES
76.B AF_APPLETALK
77T}:AppleTalk:T{
78.BR ddp (7)
79T}
80T{
d4c8c97c 81.B AF_X25
107995d7 82T}:ITU-T X.25 / ISO/IEC\~8208 protocol:T{
77117f4f
MK
83.BR x25 (7)
84T}
85T{
5880549a
ES
86.B AF_INET6
87T}:IPv6 Internet protocols:T{
88.BR ipv6 (7)
89T}
90T{
5880549a
ES
91.B AF_DECnet
92T}:T{
e900e16c 93DECet protocol sockets
5880549a 94T}
77117f4f 95T{
5880549a
ES
96.B AF_KEY
97T}:T{
698c7b2f 98Key management protocol, originally developed for usage with IPsec
5880549a
ES
99T}
100T{
101.B AF_NETLINK
102T}:T{
103Kernel user interface device
104T}:T{
105.BR netlink (7)
77117f4f
MK
106T}
107T{
d4c8c97c 108.B AF_PACKET
77117f4f 109T}:T{
e900e16c 110Low-level packet interface
77117f4f
MK
111T}:T{
112.BR packet (7)
113T}
5880549a 114T{
5880549a
ES
115.B AF_RDS
116T}:T{
117.\" commit: 639b321b4d8f4e412bfbb2a4a19bfebc1e68ace4
698c7b2f 118Reliable Datagram Sockets (RDS) protocol
5880549a
ES
119T}:T{
120.\" rds-tools: https://github.com/oracle/rds-tools/blob/master/rds.7
121.\" rds-tools: https://github.com/oracle/rds-tools/blob/master/rds-rdma.7
e900e16c
MK
122.BR rds (7)
123.br
28a4c58c 124.BR rds\-rdma (7)
5880549a
ES
125T}
126T{
5880549a
ES
127.B AF_PPPOX
128T}:T{
ff085a5e 129Generic PPP transport layer, for setting up L2 tunnels
e900e16c 130(L2TP and PPPoE)
5880549a
ES
131T}
132T{
5880549a
ES
133.B AF_LLC
134T}:T{
135.\" linux-history commit: 34beb106cde7da233d4df35dd3d6cf4fee937caa
698c7b2f 136Logical link control (IEEE 802.2 LLC) protocol
5880549a
ES
137T}
138T{
139.B AF_IB
140T}:T{
141.\" commits: 8d36eb01da5d371f..ce117ffac2e93334
698c7b2f 142InfiniBand native addressing
5880549a
ES
143T}
144T{
145.B AF_MPLS
146T}:T{
147.\" commits: 0189197f441602acdca3f97750d392a895b778fd
698c7b2f 148Multiprotocol Label Switching
5880549a
ES
149T}
150T{
151.B AF_CAN
152T}:T{
153.\" commits: 8dbde28d9711475a..5423dd67bd0108a1
698c7b2f 154Controller Area Network automotive bus protocol
5880549a
ES
155T}
156T{
157.B AF_TIPC
158T}:T{
159.\" commits: b97bf3fd8f6a16966d4f18983b2c40993ff937d4
698c7b2f 160TIPC, "cluster domain sockets" protocol
5880549a
ES
161T}
162T{
163.B AF_BLUETOOTH
164T}:T{
165.\" commits: 8d36eb01da5d371f..ce117ffac2e93334
698c7b2f 166Bluetooth low-level socket protocol
5880549a
ES
167T}
168T{
31d070f8
SM
169.B AF_ALG
170T}:T{
5880549a 171.\" commit: 03c8efc1ffeb6b82a22c1af8dd908af349563314
698c7b2f 172Interface to kernel crypto API
5880549a
ES
173T}
174T{
49c3b226
ES
175.B AF_VSOCK
176T}:T{
177.\" commit: d021c344051af91f42c5ba9fdedc176740cbd238
178VSOCK (originally "VMWare VSockets") protocol
179for hypervisor-guest communication
180T}:T{
181.BR vsock (7)
182T}
183T{
5880549a
ES
184.B AF_KCM
185T}:T{
186.\" commit: 03c8efc1ffeb6b82a22c1af8dd908af349563314
490f9b6c 187KCM (kernel connection multiplexer) interface
5880549a
ES
188T}
189T{
9eadb327
TK
190.B AF_XDP
191T}:T{
5880549a 192.\" commit: c0c77d8fb787cfe0c3fca689c2a30d1dad4eaba7
698c7b2f 193XDP (express data path) interface
9eadb327 194T}
77117f4f 195.TE
c6d039a3 196.P
43c8308e
MK
197Further details of the above address families,
198as well as information on several other address families, can be found in
199.BR address_families (7).
c6d039a3 200.P
77117f4f
MK
201The socket has the indicated
202.IR type ,
203which specifies the communication semantics.
204Currently defined types
205are:
1ee674a4 206.TP 16
77117f4f
MK
207.B SOCK_STREAM
208Provides sequenced, reliable, two-way, connection-based byte streams.
209An out-of-band data transmission mechanism may be supported.
210.TP
211.B SOCK_DGRAM
212Supports datagrams (connectionless, unreliable messages of a fixed
213maximum length).
214.TP
215.B SOCK_SEQPACKET
216Provides a sequenced, reliable, two-way connection-based data
217transmission path for datagrams of fixed maximum length; a consumer is
218required to read an entire packet with each input system call.
219.TP
220.B SOCK_RAW
221Provides raw network protocol access.
222.TP
223.B SOCK_RDM
224Provides a reliable datagram layer that does not guarantee ordering.
225.TP
226.B SOCK_PACKET
227Obsolete and should not be used in new programs;
228see
229.BR packet (7).
c6d039a3 230.P
d332f86f 231Some socket types may not be implemented by all protocol families.
c6d039a3 232.P
af14d9f8
MK
233Since Linux 2.6.27, the
234.I type
235argument serves a second purpose:
236in addition to specifying a socket type,
237it may include the bitwise OR of any of the following values,
238to modify the behavior of
239.BR socket ():
240.TP 16
241.B SOCK_NONBLOCK
242Set the
1ae6b2c7 243.B O_NONBLOCK
7f11e32c
MK
244file status flag on the open file description (see
245.BR open (2))
246referred to by the new file descriptor.
af14d9f8
MK
247Using this flag saves extra calls to
248.BR fcntl (2)
249to achieve the same result.
250.TP
251.B SOCK_CLOEXEC
252Set the close-on-exec
253.RB ( FD_CLOEXEC )
254flag on the new file descriptor.
c5571b61 255See the description of the
af14d9f8
MK
256.B O_CLOEXEC
257flag in
258.BR open (2)
259for reasons why this may be useful.
c6d039a3 260.P
77117f4f
MK
261The
262.I protocol
263specifies a particular protocol to be used with the socket.
264Normally only a single protocol exists to support a particular
265socket type within a given protocol family, in which case
266.I protocol
267can be specified as 0.
268However, it is possible that many protocols may exist, in
269which case a particular protocol must be specified in this manner.
270The protocol number to use is specific to the \*(lqcommunication domain\*(rq
271in which communication is to take place; see
272.BR protocols (5).
273See
274.BR getprotoent (3)
275on how to map protocol name strings to protocol numbers.
c6d039a3 276.P
77117f4f
MK
277Sockets of type
278.B SOCK_STREAM
f6e34058 279are full-duplex byte streams.
77117f4f
MK
280They do not preserve
281record boundaries.
282A stream socket must be in
283a
284.I connected
285state before any data may be sent or received on it.
286A connection to
287another socket is created with a
288.BR connect (2)
289call.
290Once connected, data may be transferred using
291.BR read (2)
292and
293.BR write (2)
294calls or some variant of the
295.BR send (2)
296and
297.BR recv (2)
298calls.
299When a session has been completed a
300.BR close (2)
301may be performed.
302Out-of-band data may also be transmitted as described in
303.BR send (2)
304and received as described in
305.BR recv (2).
c6d039a3 306.P
77117f4f
MK
307The communications protocols which implement a
308.B SOCK_STREAM
309ensure that data is not lost or duplicated.
310If a piece of data for which
311the peer protocol has buffer space cannot be successfully transmitted
312within a reasonable length of time, then the connection is considered
313to be dead.
314When
315.B SO_KEEPALIVE
316is enabled on the socket the protocol checks in a protocol-specific
317manner if the other end is still alive.
318A
319.B SIGPIPE
320signal is raised if a process sends or receives
321on a broken stream; this causes naive processes,
322which do not handle the signal, to exit.
323.B SOCK_SEQPACKET
324sockets employ the same system calls as
325.B SOCK_STREAM
326sockets.
327The only difference is that
328.BR read (2)
329calls will return only the amount of data requested,
330and any data remaining in the arriving packet will be discarded.
331Also all message boundaries in incoming datagrams are preserved.
c6d039a3 332.P
77117f4f
MK
333.B SOCK_DGRAM
334and
335.B SOCK_RAW
336sockets allow sending of datagrams to correspondents named in
337.BR sendto (2)
338calls.
339Datagrams are generally received with
340.BR recvfrom (2),
341which returns the next datagram along with the address of its sender.
c6d039a3 342.P
77117f4f
MK
343.B SOCK_PACKET
344is an obsolete socket type to receive raw packets directly from the
345device driver.
346Use
347.BR packet (7)
348instead.
c6d039a3 349.P
77117f4f
MK
350An
351.BR fcntl (2)
352.B F_SETOWN
353operation can be used to specify a process or process group to receive a
354.B SIGURG
355signal when the out-of-band data arrives or
356.B SIGPIPE
357signal when a
358.B SOCK_STREAM
359connection breaks unexpectedly.
360This operation may also be used to set the process or process group
361that receives the I/O and asynchronous notification of I/O events via
362.BR SIGIO .
363Using
364.B F_SETOWN
365is equivalent to an
366.BR ioctl (2)
367call with the
368.B FIOSETOWN
369or
370.B SIOCSPGRP
371argument.
c6d039a3 372.P
77117f4f 373When the network signals an error condition to the protocol module (e.g.,
b5fe8515 374using an ICMP message for IP) the pending error flag is set for the socket.
77117f4f
MK
375The next operation on this socket will return the error code of the pending
376error.
377For some protocols it is possible to enable a per-socket error queue
378to retrieve detailed information about the error; see
379.B IP_RECVERR
380in
381.BR ip (7).
c6d039a3 382.P
77117f4f
MK
383The operation of sockets is controlled by socket level
384.IR options .
385These options are defined in
386.IR <sys/socket.h> .
387The functions
388.BR setsockopt (2)
389and
390.BR getsockopt (2)
83a9c27c 391are used to set and get options.
47297adb 392.SH RETURN VALUE
77117f4f
MK
393On success, a file descriptor for the new socket is returned.
394On error, \-1 is returned, and
395.I errno
f6a4078b 396is set to indicate the error.
77117f4f
MK
397.SH ERRORS
398.TP
399.B EACCES
400Permission to create a socket of the specified type and/or protocol
401is denied.
402.TP
403.B EAFNOSUPPORT
404The implementation does not support the specified address family.
405.TP
406.B EINVAL
407Unknown protocol, or protocol family not available.
408.TP
af14d9f8
MK
409.B EINVAL
410.\" Since Linux 2.6.27
411Invalid flags in
412.IR type .
413.TP
77117f4f 414.B EMFILE
26c32fab 415The per-process limit on the number of open file descriptors has been reached.
77117f4f
MK
416.TP
417.B ENFILE
e258766b 418The system-wide limit on the total number of open files has been reached.
77117f4f
MK
419.TP
420.BR ENOBUFS " or " ENOMEM
421Insufficient memory is available.
422The socket cannot be
423created until sufficient resources are freed.
424.TP
425.B EPROTONOSUPPORT
426The protocol type or the specified protocol is not
427supported within this domain.
c6d039a3 428.P
77117f4f 429Other errors may be generated by the underlying protocol modules.
3113c7f3 430.SH STANDARDS
4131356c 431POSIX.1-2008.
c6d039a3 432.P
af14d9f8
MK
433.B SOCK_NONBLOCK
434and
435.B SOCK_CLOEXEC
4131356c
AC
436are Linux-specific.
437.SH HISTORY
438POSIX.1-2001, 4.4BSD.
c6d039a3 439.P
77117f4f
MK
440.BR socket ()
441appeared in 4.2BSD.
442It is generally portable to/from
443non-BSD systems supporting clones of the BSD socket layer (including
efbfd7ec 444System\ V variants).
c6d039a3 445.P
77117f4f
MK
446The manifest constants used under 4.x BSD for protocol families
447are
448.BR PF_UNIX ,
449.BR PF_INET ,
4a037a4a
MK
450and so on, while
451.BR AF_UNIX ,
1f1fd5ef 452.BR AF_INET ,
4a037a4a 453and so on are used for address
77117f4f
MK
454families.
455However, already the BSD man page promises: "The protocol
456family generally is the same as the address family", and subsequent
457standards use AF_* everywhere.
a14af333 458.SH EXAMPLES
77117f4f
MK
459An example of the use of
460.BR socket ()
461is shown in
462.BR getaddrinfo (3).
47297adb 463.SH SEE ALSO
77117f4f
MK
464.BR accept (2),
465.BR bind (2),
c1d0454a 466.BR close (2),
77117f4f
MK
467.BR connect (2),
468.BR fcntl (2),
469.BR getpeername (2),
470.BR getsockname (2),
471.BR getsockopt (2),
472.BR ioctl (2),
473.BR listen (2),
474.BR read (2),
475.BR recv (2),
476.BR select (2),
477.BR send (2),
478.BR shutdown (2),
479.BR socketpair (2),
480.BR write (2),
481.BR getprotoent (3),
43c8308e 482.BR address_families (7),
77117f4f
MK
483.BR ip (7),
484.BR socket (7),
485.BR tcp (7),
486.BR udp (7),
487.BR unix (7)
c6d039a3 488.P
f29fc8dc 489\[lq]An Introductory 4.3BSD Interprocess Communication Tutorial\[rq]
173fe7e7 490and
f29fc8dc 491\[lq]BSD Interprocess Communication Tutorial\[rq],
173fe7e7 492reprinted in
77117f4f 493.I UNIX Programmer's Supplementary Documents Volume 1.