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

NAME

12       socket — create an endpoint for communication
13

SYNOPSIS

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

DESCRIPTION

20       The socket() function shall create an unbound socket  in  a  communica‐
21       tions  domain,  and  return a file descriptor that can be used in later
22       function calls that operate on sockets. The file  descriptor  shall  be
23       allocated as described in Section 2.14, File Descriptor Allocation.
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       Section  2.14, File Descriptor Allocation, accept(), bind(), connect(),
126       getsockname(), getsockopt(), listen(), recv(),  recvfrom(),  recvmsg(),
127       send(), sendmsg(), setsockopt(), shutdown(), socketpair()
128
129       The   Base   Definitions   volume   of   POSIX.1‐2017,  <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-2017, Standard for Information Technology -- Por‐
135       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
136       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
137       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
138       event of any discrepancy between this version and the original IEEE and
139       The Open Group Standard, the original IEEE and The Open Group  Standard
140       is  the  referee document. The original Standard can be obtained online
141       at http://www.opengroup.org/unix/online.html .
142
143       Any typographical or formatting errors that appear  in  this  page  are
144       most likely to have been introduced during the conversion of the source
145       files to man page format. To report such errors,  see  https://www.ker
146       nel.org/doc/man-pages/reporting_bugs.html .
147
148
149
150IEEE/The Open Group                  2017                           SOCKET(3P)
Impressum