1SOCKET(3P)                 POSIX Programmer's Manual                SOCKET(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       socket — create an endpoint for communication
14

SYNOPSIS

16       #include <sys/socket.h>
17
18       int socket(int domain, int type, int protocol);
19

DESCRIPTION

21       The socket() function shall create an unbound socket  in  a  communica‐
22       tions  domain,  and  return a file descriptor that can be used in later
23       function calls that operate on sockets.
24
25       The socket() function takes the following arguments:
26
27       domain      Specifies the communications domain in which a socket is to
28                   be created.
29
30       type        Specifies the type of socket to be created.
31
32       protocol    Specifies a particular protocol to be used with the socket.
33                   Specifying a protocol  of  0  causes  socket()  to  use  an
34                   unspecified  default protocol appropriate for the requested
35                   socket type.
36
37       The domain argument specifies the address family used in the communica‐
38       tions  domain.  The address families supported by the system are imple‐
39       mentation-defined.
40
41       Symbolic constants that can be used for the domain argument are defined
42       in the <sys/socket.h> header.
43
44       The  type  argument  specifies  the  socket  type, which determines the
45       semantics of communication over the socket. The following socket  types
46       are defined; implementations may specify additional socket types:
47
48       SOCK_STREAM Provides  sequenced,  reliable,  bidirectional, connection-
49                   mode byte streams, and may provide a transmission mechanism
50                   for out-of-band data.
51
52       SOCK_DGRAM  Provides  datagrams, which are connectionless-mode, unreli‐
53                   able messages of fixed maximum length.
54
55       SOCK_SEQPACKET
56                   Provides sequenced,  reliable,  bidirectional,  connection-
57                   mode  transmission  paths for records. A record can be sent
58                   using one or more output operations and received using  one
59                   or  more  input  operations,  but  a single operation never
60                   transfers part of more than one record.  Record  boundaries
61                   are visible to the receiver via the MSG_EOR flag.
62
63       If  the protocol argument is non-zero, it shall specify a protocol that
64       is supported by the address family. If the protocol argument  is  zero,
65       the  default  protocol  for this address family and type shall be used.
66       The protocols supported by the system are implementation-defined.
67
68       The process may need to have appropriate privileges to use the socket()
69       function or to create some sockets.
70

RETURN VALUE

72       Upon  successful completion, socket() shall return a non-negative inte‐
73       ger, the socket file descriptor.  Otherwise, a value  of  −1  shall  be
74       returned and errno set to indicate the error.
75

ERRORS

77       The socket() function shall fail if:
78
79       EAFNOSUPPORT
80              The  implementation  does not support the specified address fam‐
81              ily.
82
83       EMFILE All file descriptors available  to  the  process  are  currently
84              open.
85
86       ENFILE No more file descriptors are available for the system.
87
88       EPROTONOSUPPORT
89              The protocol is not supported by the address family, or the pro‐
90              tocol is not supported by the implementation.
91
92       EPROTOTYPE
93              The socket type is not supported by the protocol.
94
95       The socket() function may fail if:
96
97       EACCES The process does not have appropriate privileges.
98
99       ENOBUFS
100              Insufficient resources were available in the system  to  perform
101              the operation.
102
103       ENOMEM Insufficient memory was available to fulfill the request.
104
105       The following sections are informative.
106

EXAMPLES

108       None.
109

APPLICATION USAGE

111       The  documentation for specific address families specifies which proto‐
112       cols each address family supports. The documentation for specific  pro‐
113       tocols specifies which socket types each protocol supports.
114
115       The application can determine whether an address family is supported by
116       trying to create a socket with domain set to the protocol in question.
117

RATIONALE

119       None.
120

FUTURE DIRECTIONS

122       None.
123

SEE ALSO

125       accept(), bind(),  connect(),  getsockname(),  getsockopt(),  listen(),
126       recv(),  recvfrom(),  recvmsg(), send(), sendmsg(), setsockopt(), shut‐
127       down(), socketpair()
128
129       The  Base   Definitions   volume   of   POSIX.1‐2008,   <netinet_in.h>,
130       <sys_socket.h>
131
133       Portions  of  this text are reprinted and reproduced in electronic form
134       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
135       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
136       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
137       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
138       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
139       event of any discrepancy between this version and the original IEEE and
140       The Open Group Standard, the original IEEE and The Open Group  Standard
141       is  the  referee document. The original Standard can be obtained online
142       at http://www.unix.org/online.html .
143
144       Any typographical or formatting errors that appear  in  this  page  are
145       most likely to have been introduced during the conversion of the source
146       files to man page format. To report such errors,  see  https://www.ker
147       nel.org/doc/man-pages/reporting_bugs.html .
148
149
150
151IEEE/The Open Group                  2013                           SOCKET(3P)
Impressum