1 .\" $NetBSD: socket.2,v 1.5 1995/02/27 12:37:53 cgd Exp $
3 .\" Copyright (c) 1983, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
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
34 .\" @(#)socket.2 8.1 (Berkeley) 6/4/93
41 .Nd create an endpoint for communication
43 .Fd #include <sys/socket.h>
52 creates an endpoint for communication and returns a descriptor.
56 parameter specifies a communications domain within which
57 communication will take place; this selects the protocol family
59 These families are defined in the include file
60 .Ao Pa sys/socket.h Ac .
61 The currently understood formats are
63 .Bd -literal -offset indent -compact
64 PF_LOCAL Host-internal protocols, formerly called PF_UNIX,
65 PF_UNIX Host-internal protocols, deprecated, use PF_LOCAL,
66 PF_INET Internet version 4 protocols,
67 PF_ROUTE Internal Routing protocol,
68 PF_KEY Internal key-management function,
69 PF_INET6 Internet version 6 protocols,
70 PF_SYSTEM System domain,
71 PF_NDRV Raw access to network device,
72 PF_VSOCK VM Sockets protocols
75 The socket has the indicated
77 which specifies the semantics of communication. Currently
80 .Bd -literal -offset indent -compact
90 type provides sequenced, reliable,
91 two-way connection based byte streams.
92 An out-of-band data transmission mechanism may be supported.
96 datagrams (connectionless, unreliable messages of
97 a fixed (typically small) maximum length).
100 .\"socket may provide a sequenced, reliable,
101 .\"two-way connection-based data transmission path for datagrams
102 .\"of fixed maximum length; a consumer may be required to read
103 .\"an entire packet with each read system call.
104 .\"This facility is protocol specific, and presently implemented
108 sockets provide access to internal network protocols and interfaces.
111 which is available only to the super-user.
114 .\" which is planned,
115 .\" but not yet implemented, are not described here.
119 specifies a particular protocol to be used with the socket.
120 Normally only a single protocol exists to support a particular
121 socket type within a given protocol family. However, it is possible
122 that many protocols may exist, in which case a particular protocol
123 must be specified in this manner. The protocol number to use is
124 particular to the \*(lqcommunication domain\*(rq in which communication
125 is to take place; see
130 are full-duplex byte streams, similar
131 to pipes. A stream socket must be in a
133 state before any data may be sent or received
134 on it. A connection to another socket is created with a
138 call. Once connected, data may be transferred using
142 calls or some variant of the
146 calls. When a session has been completed a
149 Out-of-band data may also be transmitted as described in
151 and received as described in
154 The communications protocols used to implement a
157 is not lost or duplicated. If a piece of data for which the
158 peer protocol has buffer space cannot be successfully transmitted
159 within a reasonable length of time, then
160 the connection is considered broken and calls
161 will indicate an error with
165 in the global variable
167 The protocols optionally keep sockets
169 by forcing transmissions
170 roughly every minute in the absence of other activity.
171 An error is then indicated if no response can be
172 elicited on an otherwise
173 idle connection for a extended period (e.g. 5 minutes).
176 signal is raised if a process sends
177 on a broken stream; this causes naive processes,
178 which do not handle the signal, to exit.
180 .\" .Dv SOCK_SEQPACKET
181 .\" sockets employ the same system calls
184 .\" sockets. The only difference
187 .\" calls will return only the amount of data requested,
188 .\" and any remaining in the arriving packet will be discarded.
193 sockets allow sending of datagrams to correspondents
196 calls. Datagrams are generally received with
198 which returns the next datagram with its return address.
202 call can be used to specify a process group to receive
205 signal when the out-of-band data arrives.
206 It may also enable non-blocking I/O
207 and asynchronous notification of I/O events
211 The operation of sockets is controlled by socket level
213 These options are defined in the file
214 .Ao Pa sys/socket.h Ac .
218 are used to set and get options, respectively.
220 A -1 is returned if an error occurs, otherwise the return
221 value is a descriptor referencing the socket.
225 system call fails if:
229 Permission to create a socket of the specified type and/or protocol
232 .It Bq Er EAFNOSUPPORT
233 The specified address family is not supported.
236 The per-process descriptor table is full.
239 The system file table is full.
242 Insufficient buffer space is available.
243 The socket cannot be created until sufficient resources are freed.
246 Insufficient memory was available to fulfill the request.
248 .It Bq Er EPROTONOSUPPORT
249 The protocol type or the specified protocol is not supported
253 The socket type is not supported by the protocol.
256 If a new protocol family is defined,
257 the socreate process is free to return any desired error code.
260 system call will pass this error code along
261 (even if it is undefined).
263 .Fd #include <sys/types.h>
264 .Fd #include <sys/socket.h>
292 .%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
293 .%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
296 .%T "BSD Interprocess Communication Tutorial"
297 .%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
302 function call appeared in