1 .\" $NetBSD: socket.2,v 1.41 2013/03/01 18:25:16 joerg 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. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)socket.2 8.1 (Berkeley) 6/4/93
37 .Nd create an endpoint for communication
43 .Fn socket "int domain" "int type" "int protocol"
46 creates an endpoint for communication and returns a descriptor.
50 parameter 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
54 .Ao Pa sys/socket.h Ac .
55 The currently understood formats are:
57 .Bd -literal -offset indent -compact
58 PF_LOCAL local (previously UNIX) domain protocols
59 PF_INET ARPA Internet protocols
60 PF_INET6 IPv6 (Internet Protocol version 6) protocols
61 PF_NS Xerox Network Systems protocols
62 PF_APPLETALK AppleTalk protocols
63 PF_BLUETOOTH Bluetooth protocols
66 The socket has the indicated
68 which specifies the semantics of communication.
69 Currently defined types are:
71 .Bd -literal -offset indent -compact
79 The following flags can be or'ed to the type to condition the returned
81 The following flags are valid:
82 .Bl -column SOCK_NONBLOCK -offset indent
84 Set the close on exec property.
86 Sets non-blocking I/O.
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 is protocol specific, and presently implemented
114 sockets provide access to internal network protocols and interfaces.
117 which is available only to the super-user, and
120 but not yet implemented, are not described here.
124 specifies a particular protocol to be used with the socket.
125 Normally only a single protocol exists to support a particular
126 socket type within a given protocol family.
127 However, it is possible that many protocols may exist, in which case
128 a particular protocol must be specified in this manner.
129 The protocol number to use is
130 particular to the \*(lqcommunication domain\*(rq in which communication
131 is to take place; see
136 are full-duplex byte streams.
137 A stream socket must be in a
139 state before any data may be sent or received
141 A connection to another socket is created with a
144 Once connected, data may be transferred using
148 calls or some variant of the
153 When a session has been completed a
156 Out-of-band data may also be transmitted as described in
158 and received as described in
161 The communications protocols used to implement a
164 is not lost or duplicated.
165 If a piece of data for which the
166 peer protocol has buffer space cannot be successfully transmitted
167 within a reasonable length of time, then
168 the connection is considered broken and calls
169 will indicate an error with
173 in the global variable
175 The protocols optionally keep sockets
177 by forcing transmissions
178 roughly every minute in the absence of other activity.
179 An error is then indicated if no response can be
180 elicited on an otherwise
181 idle connection for an extended period (e.g., 5 minutes).
184 signal is raised if a process sends
185 on a broken stream; this causes naive processes,
186 which do not handle the signal, to exit.
189 sockets employ the same system calls
193 The only difference is that
195 calls will return only the amount of data requested,
196 and any remaining in the arriving packet will be discarded.
201 sockets allow sending of datagrams to correspondents
205 Datagrams are generally received with
207 which returns the next datagram with its return address.
211 call can be used to specify a process group to receive
214 signal when the out-of-band data arrives.
215 It may also enable non-blocking I/O
216 and asynchronous notification of I/O events
220 The operation of sockets is controlled by socket level
222 These options are defined in the file
223 .Ao Pa sys/socket.h Ac .
228 system calls are used to set and get options, respectively.
230 A \-1 is returned if an error occurs, otherwise the return
231 value is a descriptor referencing the socket.
238 Permission to create a socket of the specified type and/or protocol
240 .It Bq Er EAFNOSUPPORT
241 The address family (domain) is not supported or
242 the specified domain is not supported by this protocol family.
244 The per-process descriptor table is full.
246 The system file table is full.
248 Insufficient buffer space is available.
249 The socket cannot be created until sufficient resources are freed.
250 .It Bq Er EPROTONOSUPPORT
251 The protocol family is not supported or
252 the specified protocol is not supported within this domain.
254 The socket type is not supported by the protocol.
275 .%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
278 .Pq see Pa /usr/share/doc/psd/20.ipctut
280 .%T "Advanced 4.4BSD IPC Tutorial"
281 .%A Samuel J. Leffler
288 .Pq see Pa /usr/share/doc/psd/21.ipc
292 function call appeared in