2 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
3 .\" All rights reserved.
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" $Id: socket.2,v 1.4 1999/05/13 11:33:42 freitag Exp $
37 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
38 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
39 .\" Modified 1998, 1999 by Andi Kleen <ak@muc.de>
40 .\" Modified 2002-07-17 by Michael Kerrisk <mtk.manpages@gmail.com>
41 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
43 .TH SOCKET 2 2015-02-01 "Linux" "Linux Programmer's Manual"
45 socket \- create an endpoint for communication
47 .BR "#include <sys/types.h>" " /* See NOTES */"
49 .B #include <sys/socket.h>
51 .BI "int socket(int " domain ", int " type ", int " protocol );
54 creates an endpoint for communication and returns a descriptor.
58 argument specifies a communication domain; this selects the protocol
59 family which will be used for communication.
60 These families are defined in
62 The currently understood formats include:
68 .BR AF_UNIX ", " AF_LOCAL
76 T}:IPv4 Internet protocols:T{
81 T}:IPv6 Internet protocols:T{
86 T}:IPX \- Novell protocols:
90 Kernel user interface device
96 T}:ITU-T X.25 / ISO-8208 protocol:T{
102 Amateur radio AX.25 protocol
106 T}:Access to raw ATM PVCs:
115 Low level packet interface
122 Interface to kernel crypto API
126 The socket has the indicated
128 which specifies the communication semantics.
129 Currently defined types
133 Provides sequenced, reliable, two-way, connection-based byte streams.
134 An out-of-band data transmission mechanism may be supported.
137 Supports datagrams (connectionless, unreliable messages of a fixed
141 Provides a sequenced, reliable, two-way connection-based data
142 transmission path for datagrams of fixed maximum length; a consumer is
143 required to read an entire packet with each input system call.
146 Provides raw network protocol access.
149 Provides a reliable datagram layer that does not guarantee ordering.
152 Obsolete and should not be used in new programs;
156 Some socket types may not be implemented by all protocol families.
158 Since Linux 2.6.27, the
160 argument serves a second purpose:
161 in addition to specifying a socket type,
162 it may include the bitwise OR of any of the following values,
163 to modify the behavior of
169 file status flag on the new open file description.
170 Using this flag saves extra calls to
172 to achieve the same result.
175 Set the close-on-exec
177 flag on the new file descriptor.
178 See the description of the
182 for reasons why this may be useful.
186 specifies a particular protocol to be used with the socket.
187 Normally only a single protocol exists to support a particular
188 socket type within a given protocol family, in which case
190 can be specified as 0.
191 However, it is possible that many protocols may exist, in
192 which case a particular protocol must be specified in this manner.
193 The protocol number to use is specific to the \*(lqcommunication domain\*(rq
194 in which communication is to take place; see
198 on how to map protocol name strings to protocol numbers.
202 are full-duplex byte streams.
205 A stream socket must be in
208 state before any data may be sent or received on it.
210 another socket is created with a
213 Once connected, data may be transferred using
217 calls or some variant of the
222 When a session has been completed a
225 Out-of-band data may also be transmitted as described in
227 and received as described in
230 The communications protocols which implement a
232 ensure that data is not lost or duplicated.
233 If a piece of data for which
234 the peer protocol has buffer space cannot be successfully transmitted
235 within a reasonable length of time, then the connection is considered
239 is enabled on the socket the protocol checks in a protocol-specific
240 manner if the other end is still alive.
243 signal is raised if a process sends or receives
244 on a broken stream; this causes naive processes,
245 which do not handle the signal, to exit.
247 sockets employ the same system calls as
250 The only difference is that
252 calls will return only the amount of data requested,
253 and any data remaining in the arriving packet will be discarded.
254 Also all message boundaries in incoming datagrams are preserved.
259 sockets allow sending of datagrams to correspondents named in
262 Datagrams are generally received with
264 which returns the next datagram along with the address of its sender.
267 is an obsolete socket type to receive raw packets directly from the
276 operation can be used to specify a process or process group to receive a
278 signal when the out-of-band data arrives or
282 connection breaks unexpectedly.
283 This operation may also be used to set the process or process group
284 that receives the I/O and asynchronous notification of I/O events via
296 When the network signals an error condition to the protocol module (e.g.,
297 using a ICMP message for IP) the pending error flag is set for the socket.
298 The next operation on this socket will return the error code of the pending
300 For some protocols it is possible to enable a per-socket error queue
301 to retrieve detailed information about the error; see
306 The operation of sockets is controlled by socket level
308 These options are defined in
314 are used to set and get options, respectively.
316 On success, a file descriptor for the new socket is returned.
317 On error, \-1 is returned, and
319 is set appropriately.
323 Permission to create a socket of the specified type and/or protocol
327 The implementation does not support the specified address family.
330 Unknown protocol, or protocol family not available.
333 .\" Since Linux 2.6.27
338 Process file table overflow.
341 The system limit on the total number of open files has been reached.
343 .BR ENOBUFS " or " ENOMEM
344 Insufficient memory is available.
346 created until sufficient resources are freed.
349 The protocol type or the specified protocol is not
350 supported within this domain.
352 Other errors may be generated by the underlying protocol modules.
354 4.4BSD, POSIX.1-2001.
360 flags are Linux-specific.
364 It is generally portable to/from
365 non-BSD systems supporting clones of the BSD socket layer (including
368 POSIX.1-2001 does not require the inclusion of
370 and this header file is not required on Linux.
371 However, some historical (BSD) implementations required this header
372 file, and portable applications are probably wise to include it.
374 The manifest constants used under 4.x BSD for protocol families
381 and so on are used for address
383 However, already the BSD man page promises: "The protocol
384 family generally is the same as the address family", and subsequent
385 standards use AF_* everywhere.
389 protocol type was added in Linux 2.6.38.
390 More information on this interface is provided in the kernel source file,
391 .IR Documentation/crypto/crypto-API-userspace.txt .
393 An example of the use of
421 \(lqAn Introductory 4.3BSD Interprocess Communication Tutorial\(rq
423 \(lqBSD Interprocess Communication Tutorial\(rq,
425 .I UNIX Programmer's Supplementary Documents Volume 1.
427 This page is part of release 3.79 of the Linux
430 A description of the project,
431 information about reporting bugs,
432 and the latest version of this page,
434 \%http://www.kernel.org/doc/man\-pages/.