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