1SOCKET(2)                  Linux Programmer's Manual                 SOCKET(2)
2
3
4

NAME

6       socket - create an endpoint for communication
7

SYNOPSIS

9       #include <sys/types.h>          /* See NOTES */
10       #include <sys/socket.h>
11
12       int socket(int domain, int type, int protocol);
13

DESCRIPTION

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 formats currently understood by the
23       Linux kernel include:
24
25       Name         Purpose                                    Man page
26       AF_UNIX      Local communication                        unix(7)
27       AF_LOCAL     Synonym for AF_UNIX
28       AF_INET      IPv4 Internet protocols                    ip(7)
29       AF_AX25      Amateur radio AX.25 protocol               ax25(4)
30       AF_IPX       IPX - Novell protocols
31       AF_APPLETALK AppleTalk                                  ddp(7)
32       AF_X25       ITU-T X.25 / ISO-8208 protocol             x25(7)
33       AF_INET6     IPv6 Internet protocols                    ipv6(7)
34       AF_DECnet    DECet protocol sockets
35       AF_KEY       Key   management   protocol,  originally
36                    developed for usage with IPsec
37       AF_NETLINK   Kernel user interface device               netlink(7)
38       AF_PACKET    Low-level packet interface                 packet(7)
39       AF_RDS       Reliable Datagram Sockets (RDS) protocol   rds(7)
40                                                               rds-rdma(7)
41       AF_PPPOX     Generic PPP transport layer, for setting
42                    up L2 tunnels (L2TP and PPPoE)
43       AF_LLC       Logical  link  control  (IEEE 802.2 LLC)
44                    protocol
45       AF_IB        InfiniBand native addressing
46       AF_MPLS      Multiprotocol Label Switching
47       AF_CAN       Controller Area Network  automotive  bus
48                    protocol
49       AF_TIPC      TIPC, "cluster domain sockets" protocol
50       AF_BLUETOOTH Bluetooth low-level socket protocol
51       AF_ALG       Interface to kernel crypto API
52       AF_VSOCK     VSOCK   (originally  "VMWare  VSockets")   vsock(7)
53                    protocol for hypervisor-guest communica‐
54                    tion
55       AF_KCM       KCM   (kernel   connection  multiplexor)
56                    interface
57       AF_XDP       XDP (express data path) interface
58
59       Further details of the above address families, as well  as  information
60       on several other address families, can be found in address_families(7).
61
62       The  socket  has  the indicated type, which specifies the communication
63       semantics.  Currently defined types are:
64
65       SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based
66                       byte  streams.  An out-of-band data transmission mecha‐
67                       nism may be supported.
68
69       SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages
70                       of a fixed maximum length).
71
72       SOCK_SEQPACKET  Provides  a  sequenced,  reliable,  two-way connection-
73                       based data transmission path  for  datagrams  of  fixed
74                       maximum  length;  a  consumer  is  required  to read an
75                       entire packet with each input system call.
76
77       SOCK_RAW        Provides raw network protocol access.
78
79       SOCK_RDM        Provides a reliable datagram layer that does not  guar‐
80                       antee ordering.
81
82       SOCK_PACKET     Obsolete  and  should  not be used in new programs; see
83                       packet(7).
84
85       Some socket types may not be implemented by all protocol families.
86
87       Since Linux 2.6.27, the type argument serves a second purpose: in addi‐
88       tion  to specifying a socket type, it may include the bitwise OR of any
89       of the following values, to modify the behavior of socket():
90
91       SOCK_NONBLOCK   Set the O_NONBLOCK file status flag on  the  open  file
92                       description  (see  open(2)) referred to by the new file
93                       descriptor.  Using  this  flag  saves  extra  calls  to
94                       fcntl(2) to achieve the same result.
95
96       SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new file
97                       descriptor.  See the description of the O_CLOEXEC  flag
98                       in open(2) for reasons why this may be useful.
99
100       The  protocol  specifies  a  particular  protocol  to  be used with the
101       socket.  Normally only a single protocol exists to support a particular
102       socket  type within a given protocol family, in which case protocol can
103       be specified as 0.  However, it is possible  that  many  protocols  may
104       exist,  in  which  case a particular protocol must be specified in this
105       manner.  The protocol number to use is specific to  the  “communication
106       domain” in which communication is to take place; see protocols(5).  See
107       getprotoent(3) on how to map protocol name strings to protocol numbers.
108
109       Sockets of type SOCK_STREAM are full-duplex byte streams.  They do  not
110       preserve  record  boundaries.   A  stream socket must be in a connected
111       state before any data may be sent or received on it.  A  connection  to
112       another socket is created with a connect(2) call.  Once connected, data
113       may be transferred using read(2) and write(2) calls or some variant  of
114       the  send(2)  and  recv(2)  calls.  When a session has been completed a
115       close(2) may be performed.  Out-of-band data may also be transmitted as
116       described in send(2) and received as described in recv(2).
117
118       The  communications protocols which implement a SOCK_STREAM ensure that
119       data is not lost or duplicated.  If a piece of data for which the  peer
120       protocol  has  buffer space cannot be successfully transmitted within a
121       reasonable length of time, then the  connection  is  considered  to  be
122       dead.   When  SO_KEEPALIVE is enabled on the socket the protocol checks
123       in a protocol-specific manner if the other end is still alive.  A  SIG‐
124       PIPE  signal  is  raised  if  a  process  sends or receives on a broken
125       stream; this causes naive processes, which do not handle the signal, to
126       exit.    SOCK_SEQPACKET   sockets  employ  the  same  system  calls  as
127       SOCK_STREAM sockets.  The only difference is that  read(2)  calls  will
128       return only the amount of data requested, and any data remaining in the
129       arriving packet will be discarded.   Also  all  message  boundaries  in
130       incoming datagrams are preserved.
131
132       SOCK_DGRAM  and  SOCK_RAW  sockets allow sending of datagrams to corre‐
133       spondents named in sendto(2) calls.  Datagrams are  generally  received
134       with  recvfrom(2),  which  returns  the  next  datagram  along with the
135       address of its sender.
136
137       SOCK_PACKET is an obsolete socket type to receive raw packets  directly
138       from the device driver.  Use packet(7) instead.
139
140       An  fcntl(2)  F_SETOWN  operation  can  be used to specify a process or
141       process group to receive a SIGURG  signal  when  the  out-of-band  data
142       arrives  or  SIGPIPE  signal when a SOCK_STREAM connection breaks unex‐
143       pectedly.  This operation may also  be  used  to  set  the  process  or
144       process  group  that  receives the I/O and asynchronous notification of
145       I/O events via SIGIO.  Using F_SETOWN is equivalent to an ioctl(2) call
146       with the FIOSETOWN or SIOCSPGRP argument.
147
148       When  the  network  signals  an  error condition to the protocol module
149       (e.g., using an ICMP message for IP) the pending error flag is set  for
150       the  socket.   The  next operation on this socket will return the error
151       code of the pending error.  For some protocols it is possible to enable
152       a  per-socket  error  queue  to retrieve detailed information about the
153       error; see IP_RECVERR in ip(7).
154
155       The operation of sockets is controlled by socket level options.   These
156       options are defined in <sys/socket.h>.  The functions setsockopt(2) and
157       getsockopt(2) are used to set and get options.
158

RETURN VALUE

160       On success, a file descriptor for  the  new  socket  is  returned.   On
161       error, -1 is returned, and errno is set appropriately.
162

ERRORS

164       EACCES Permission  to create a socket of the specified type and/or pro‐
165              tocol is denied.
166
167       EAFNOSUPPORT
168              The implementation does not support the specified  address  fam‐
169              ily.
170
171       EINVAL Unknown protocol, or protocol family not available.
172
173       EINVAL Invalid flags in type.
174
175       EMFILE The per-process limit on the number of open file descriptors has
176              been reached.
177
178       ENFILE The system-wide limit on the total number of open files has been
179              reached.
180
181       ENOBUFS or ENOMEM
182              Insufficient  memory is available.  The socket cannot be created
183              until sufficient resources are freed.
184
185       EPROTONOSUPPORT
186              The protocol type or the specified  protocol  is  not  supported
187              within this domain.
188
189       Other errors may be generated by the underlying protocol modules.
190

CONFORMING TO

192       POSIX.1-2001, POSIX.1-2008, 4.4BSD.
193
194       The SOCK_NONBLOCK and SOCK_CLOEXEC flags are Linux-specific.
195
196       socket()  appeared in 4.2BSD.  It is generally portable to/from non-BSD
197       systems supporting clones of the BSD socket layer  (including  System V
198       variants).
199

NOTES

201       POSIX.1  does  not  require  the  inclusion  of <sys/types.h>, and this
202       header file is not required on Linux.  However, some  historical  (BSD)
203       implementations  required  this  header file, and portable applications
204       are probably wise to include it.
205
206       The manifest constants used under 4.x BSD  for  protocol  families  are
207       PF_UNIX, PF_INET, and so on, while AF_UNIX, AF_INET, and so on are used
208       for address families.  However, already the BSD man page promises: "The
209       protocol  family generally is the same as the address family", and sub‐
210       sequent standards use AF_* everywhere.
211

EXAMPLE

213       An example of the use of socket() is shown in getaddrinfo(3).
214

SEE ALSO

216       accept(2), bind(2),  close(2),  connect(2),  fcntl(2),  getpeername(2),
217       getsockname(2),  getsockopt(2),  ioctl(2), listen(2), read(2), recv(2),
218       select(2),  send(2),  shutdown(2),  socketpair(2),  write(2),   getpro‐
219       toent(3),   address_families(7),   ip(7),  socket(7),  tcp(7),  udp(7),
220       unix(7)
221
222       “An Introductory 4.3BSD Interprocess Communication Tutorial”  and  “BSD
223       Interprocess  Communication  Tutorial”,  reprinted in UNIX Programmer's
224       Supplementary Documents Volume 1.
225

COLOPHON

227       This page is part of release 5.04 of the Linux  man-pages  project.   A
228       description  of  the project, information about reporting bugs, and the
229       latest    version    of    this    page,    can     be     found     at
230       https://www.kernel.org/doc/man-pages/.
231
232
233
234Linux                             2019-03-06                         SOCKET(2)
Impressum