]> git.saurik.com Git - apple/xnu.git/blob - bsd/man/man2/socket.2
xnu-517.tar.gz
[apple/xnu.git] / bsd / man / man2 / socket.2
1 .\" $NetBSD: socket.2,v 1.5 1995/02/27 12:37:53 cgd Exp $
2 .\"
3 .\" Copyright (c) 1983, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
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.
21 .\"
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
32 .\" SUCH DAMAGE.
33 .\"
34 .\" @(#)socket.2 8.1 (Berkeley) 6/4/93
35 .\"
36 .Dd June 4, 1993
37 .Dt SOCKET 2
38 .Os BSD 4.2
39 .Sh NAME
40 .Nm socket
41 .Nd create an endpoint for communication
42 .Sh SYNOPSIS
43 .Fd #include <sys/types.h>
44 .Fd #include <sys/socket.h>
45 .Ft int
46 .Fn socket "int domain" "int type" "int protocol"
47 .Sh DESCRIPTION
48 .Fn Socket
49 creates an endpoint for communication and returns a descriptor.
50 .Pp
51 The
52 .Fa domain
53 parameter specifies a communications domain within which
54 communication will take place; this selects the protocol family
55 which should be used.
56 These families are defined in the include file
57 .Ao Pa sys/socket.h Ac .
58 The currently understood formats are
59 .Pp
60 .Bd -literal -offset indent -compact
61 AF_UNIX (UNIX internal protocols),
62 AF_INET (ARPA Internet protocols),
63 AF_ISO (ISO protocols),
64 AF_NS (Xerox Network Systems protocols), and
65 AF_IMPLINK (IMP \*(lqhost at IMP\*(rq link layer).
66 .Ed
67 .Pp
68 The socket has the indicated
69 .Fa type ,
70 which specifies the semantics of communication. Currently
71 defined types are:
72 .Pp
73 .Bd -literal -offset indent -compact
74 SOCK_STREAM
75 SOCK_DGRAM
76 SOCK_RAW
77 SOCK_SEQPACKET
78 SOCK_RDM
79 .Ed
80 .Pp
81 A
82 .Dv SOCK_STREAM
83 type provides sequenced, reliable,
84 two-way connection based byte streams.
85 An out-of-band data transmission mechanism may be supported.
86 A
87 .Dv SOCK_DGRAM
88 socket supports
89 datagrams (connectionless, unreliable messages of
90 a fixed (typically small) maximum length).
91 A
92 .Dv SOCK_SEQPACKET
93 socket may provide a sequenced, reliable,
94 two-way connection-based data transmission path for datagrams
95 of fixed maximum length; a consumer may be required to read
96 an entire packet with each read system call.
97 This facility is protocol specific, and presently implemented
98 only for
99 .Dv PF_NS .
100 .Dv SOCK_RAW
101 sockets provide access to internal network protocols and interfaces.
102 The types
103 .Dv SOCK_RAW ,
104 which is available only to the super-user, and
105 .Dv SOCK_RDM ,
106 which is planned,
107 but not yet implemented, are not described here.
108 .Pp
109 The
110 .Fa protocol
111 specifies a particular protocol to be used with the socket.
112 Normally only a single protocol exists to support a particular
113 socket type within a given protocol family. However, it is possible
114 that many protocols may exist, in which case a particular protocol
115 must be specified in this manner. The protocol number to use is
116 particular to the \*(lqcommunication domain\*(rq in which communication
117 is to take place; see
118 .Xr protocols 5 .
119 .Pp
120 Sockets of type
121 .Dv SOCK_STREAM
122 are full-duplex byte streams, similar
123 to pipes. A stream socket must be in a
124 .Em connected
125 state before any data may be sent or received
126 on it. A connection to another socket is created with a
127 .Xr connect 2
128 call. Once connected, data may be transferred using
129 .Xr read 2
130 and
131 .Xr write 2
132 calls or some variant of the
133 .Xr send 2
134 and
135 .Xr recv 2
136 calls. When a session has been completed a
137 .Xr close 2
138 may be performed.
139 Out-of-band data may also be transmitted as described in
140 .Xr send 2
141 and received as described in
142 .Xr recv 2 .
143 .Pp
144 The communications protocols used to implement a
145 .Dv SOCK_STREAM
146 insure that data
147 is not lost or duplicated. If a piece of data for which the
148 peer protocol has buffer space cannot be successfully transmitted
149 within a reasonable length of time, then
150 the connection is considered broken and calls
151 will indicate an error with
152 -1 returns and with
153 .Dv ETIMEDOUT
154 as the specific code
155 in the global variable
156 .Va errno .
157 The protocols optionally keep sockets
158 .Dq warm
159 by forcing transmissions
160 roughly every minute in the absence of other activity.
161 An error is then indicated if no response can be
162 elicited on an otherwise
163 idle connection for a extended period (e.g. 5 minutes).
164 A
165 .Dv SIGPIPE
166 signal is raised if a process sends
167 on a broken stream; this causes naive processes,
168 which do not handle the signal, to exit.
169 .Pp
170 .Dv SOCK_SEQPACKET
171 sockets employ the same system calls
172 as
173 .Dv SOCK_STREAM
174 sockets. The only difference
175 is that
176 .Xr read 2
177 calls will return only the amount of data requested,
178 and any remaining in the arriving packet will be discarded.
179 .Pp
180 .Dv SOCK_DGRAM
181 and
182 .Dv SOCK_RAW
183 sockets allow sending of datagrams to correspondents
184 named in
185 .Xr send 2
186 calls. Datagrams are generally received with
187 .Xr recvfrom 2 ,
188 which returns the next datagram with its return address.
189 .Pp
190 An
191 .Xr fcntl 2
192 call can be used to specify a process group to receive
193 a
194 .Dv SIGURG
195 signal when the out-of-band data arrives.
196 It may also enable non-blocking I/O
197 and asynchronous notification of I/O events
198 via
199 .Dv SIGIO .
200 .Pp
201 The operation of sockets is controlled by socket level
202 .Em options .
203 These options are defined in the file
204 .Ao Pa sys/socket.h Ac .
205 .Xr Setsockopt 2
206 and
207 .Xr getsockopt 2
208 are used to set and get options, respectively.
209 .Sh RETURN VALUES
210 A -1 is returned if an error occurs, otherwise the return
211 value is a descriptor referencing the socket.
212 .Sh ERRORS
213 The
214 .Fn socket
215 call fails if:
216 .Bl -tag -width Er
217 .It Bq Er EPROTONOSUPPORT
218 The protocol type or the specified protocol is not supported
219 within this domain.
220 .It Bq Er EMFILE
221 The per-process descriptor table is full.
222 .It Bq Er ENFILE
223 The system file table is full.
224 .It Bq Er EACCES
225 Permission to create a socket of the specified type and/or protocol
226 is denied.
227 .It Bq Er ENOBUFS
228 Insufficient buffer space is available.
229 The socket cannot be created until sufficient resources are freed.
230 .El
231 .Sh SEE ALSO
232 .Xr accept 2 ,
233 .Xr bind 2 ,
234 .Xr connect 2 ,
235 .Xr getprotoent 3 ,
236 .Xr getsockname 2 ,
237 .Xr getsockopt 2 ,
238 .Xr ioctl 2 ,
239 .Xr listen 2 ,
240 .Xr read 2 ,
241 .Xr recv 2 ,
242 .Xr select 2 ,
243 .Xr send 2 ,
244 .Xr shutdown 2 ,
245 .Xr socketpair 2 ,
246 .Xr write 2
247 .Rs
248 .%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
249 .%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
250 .Re
251 .Rs
252 .%T "BSD Interprocess Communication Tutorial"
253 .%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
254 .Re
255 .Sh HISTORY
256 The
257 .Fn socket
258 function call appeared in
259 .Bx 4.2 .