1 .\" Copyright (c) 1983, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" From: @(#)socket.2 8.1 (Berkeley) 6/4/93
35 .Nd create an endpoint for communication
41 .Fn socket "int domain" "int type" "int protocol"
46 creates an endpoint for communication and returns a descriptor.
50 argument specifies a communications domain within which
51 communication will take place; this selects the protocol family
53 These families are defined in the include file
55 The currently understood formats are:
57 .Bd -literal -offset indent -compact
58 PF_LOCAL Host-internal protocols (alias for PF_UNIX),
59 PF_UNIX Host-internal protocols,
60 PF_INET Internet version 4 protocols,
61 PF_INET6 Internet version 6 protocols,
62 PF_DIVERT Firewall packet diversion/re-injection,
63 PF_ROUTE Internal routing protocol,
64 PF_KEY Internal key-management function,
65 PF_NETGRAPH Netgraph sockets,
66 PF_NETLINK Netlink protocols,
67 PF_BLUETOOTH Bluetooth protocols,
68 PF_INET_SDP OFED socket direct protocol (IPv4),
69 AF_HYPERV HyperV sockets
72 Each protocol family is connected to an address family, which has the
73 same name except that the prefix is
77 Other protocol families may be also defined, beginning with
79 with corresponding address families.
81 The socket has the indicated
83 which specifies the semantics of communication.
87 .Bd -literal -offset indent -compact
88 SOCK_STREAM Stream socket,
89 SOCK_DGRAM Datagram socket,
90 SOCK_RAW Raw-protocol interface,
91 SOCK_SEQPACKET Sequenced packet stream
96 type provides sequenced, reliable,
97 two-way connection based byte streams.
98 An out-of-band data transmission mechanism may be supported.
102 datagrams (connectionless, unreliable messages of
103 a fixed (typically small) maximum length).
106 socket may provide a sequenced, reliable,
107 two-way connection-based data transmission path for datagrams
108 of fixed maximum length; a consumer may be required to read
109 an entire packet with each read system call.
110 This facility may have protocol-specific properties.
112 sockets provide access to internal network protocols and interfaces.
115 type is available only to the super-user and is described in
120 Additionally, the following flags are allowed in the
124 .Bd -literal -offset indent -compact
125 SOCK_CLOEXEC Set close-on-exec on the new descriptor,
126 SOCK_NONBLOCK Set non-blocking mode on the new socket
132 specifies a particular protocol to be used with the socket.
133 Normally only a single protocol exists to support a particular
134 socket type within a given protocol family.
135 However, it is possible
136 that many protocols may exist, in which case a particular protocol
137 must be specified in this manner.
138 The protocol number to use is
140 .Dq "communication domain"
141 in which communication
142 is to take place; see
147 argument may be set to zero (0) to request the default
148 implementation of a socket type for the protocol, if any.
152 are full-duplex byte streams, similar
154 A stream socket must be in a
156 state before any data may be sent or received
158 A connection to another socket is created with a
161 Once connected, data may be transferred using
165 calls or some variant of the
170 (Some protocol families, such as the Internet family,
171 support the notion of an
172 .Dq implied connect ,
173 which permits data to be sent piggybacked onto a connect operation by
177 When a session has been completed a
180 Out-of-band data may also be transmitted as described in
182 and received as described in
185 The communications protocols used to implement a
188 is not lost or duplicated.
189 If a piece of data for which the
190 peer protocol has buffer space cannot be successfully transmitted
191 within a reasonable length of time, then
192 the connection is considered broken and calls
193 will indicate an error with
197 in the global variable
199 The protocols optionally keep sockets
201 by forcing transmissions
202 roughly every minute in the absence of other activity.
203 An error is then indicated if no response can be
204 elicited on an otherwise
205 idle connection for an extended period (e.g.\& 5 minutes).
208 signal is raised if a process sends
209 on a broken stream, but this behavior may be inhibited via
213 sockets employ the same system calls
220 calls will return only the amount of data requested,
221 and any remaining in the arriving packet will be discarded.
226 sockets allow sending of datagrams to correspondents
230 Datagrams are generally received with
232 which returns the next datagram with its return address.
236 system call can be used to specify a process group to receive
239 signal when the out-of-band data arrives.
240 It may also enable non-blocking I/O
241 and asynchronous notification of I/O events
245 The operation of sockets is controlled by socket level
247 These options are defined in the file
253 system calls are used to set and get options, respectively.
255 A -1 is returned if an error occurs, otherwise the return
256 value is a descriptor referencing the socket.
260 system call fails if:
263 Permission to create a socket of the specified type and/or protocol
265 .It Bq Er EAFNOSUPPORT
266 The address family (domain) is not supported or the
267 specified domain is not supported by this protocol family.
269 The per-process descriptor table is full.
271 The system file table is full.
273 Insufficient buffer space is available.
274 The socket cannot be created until sufficient resources are freed.
276 User has insufficient privileges to carry out the requested operation.
277 .It Bq Er EPROTONOSUPPORT
278 The protocol type or the specified protocol is not supported
281 The socket type is not supported by the protocol.
307 .%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
312 .%T "BSD Interprocess Communication Tutorial"
323 standard specifies only the
328 constants for address families, and requires the use of
336 flag is expected to conform to the next revision of the
344 constants, and other address families are
350 system call appeared in