Add copyright
[unix-history] / usr / src / lib / libc / sys / socket.2
CommitLineData
39582001
KM
1.\" Copyright (c) 1983 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
af71be09 5.\" @(#)socket.2 6.1 (Berkeley) %G%
39582001 6.\"
af71be09 7.TH SOCKET 2 ""
39582001
KM
8.UC 5
9.SH NAME
10socket \- create an endpoint for communication
11.SH SYNOPSIS
12.nf
13.ft B
14#include <sys/types.h>
15#include <sys/socket.h>
16.PP
17.ft B
18s = socket(af, type, protocol)
19int s, af, type, protocol;
20.fi
21.SH DESCRIPTION
22.I Socket
23creates an endpoint for communication and returns a descriptor.
24.PP
25The
26.I af
27parameter specifies an address format with which addresses specified
28in later operations using the socket should be interpreted. These
29formats are defined in the include file
30.IR <sys/socket.h> .
31The currently understood formats are
32.PP
33.RS
34.nf
35.ta 1.25i 1.75i
36AF_UNIX (UNIX path names),
37AF_INET (ARPA Internet addresses),
38AF_PUP (Xerox PUP-I Internet addresses), and
39AF_IMPLINK (IMP \*(lqhost at IMP\*(rq addresses).
40.fi
41.RE
42.PP
43The socket has the indicated
44.I type
45which specifies the semantics of communication. Currently
46defined types are:
47.PP
48.RS
49.nf
50SOCK_STREAM
51SOCK_DGRAM
52SOCK_RAW
53SOCK_SEQPACKET
54SOCK_RDM
55.fi
56.RE
57.PP
58A SOCK_STREAM type provides sequenced, reliable,
59two-way connection based byte streams with an out-of-band data
60transmission mechanism.
61A SOCK_DGRAM socket supports
62datagrams (connectionless, unreliable messages of
63a fixed (typically small) maximum length).
64SOCK_RAW sockets provide access to internal network interfaces.
65The types SOCK_RAW,
66which is available only to the super-user, and
67SOCK_SEQPACKET and SOCK_RDM, which are planned,
68but not yet implemented, are not described here.
69.PP
70The
71.I protocol
72specifies a particular protocol to be used with the socket.
73Normally only a single protocol exists to support a particular
74socket type using a given address format. However, it is possible
75that many protocols may exist in which case a particular protocol
76must be specified in this manner. The protocol number to use is
77particular to the \*(lqcommunication domain\*(rq in which communication
78is to take place; see
79.IR services (3N)
80and
81.IR protocols (3N).
82.PP
83Sockets of type SOCK_STREAM
84are full-duplex byte streams, similar
85to pipes. A stream socket must be in a
86.I connected
87state before any data may be sent or received
88on it. A connection to another socket is created with a
89.IR connect (2)
90call. Once connected, data may be transferred using
91.IR read (2)
92and
93.IR write (2)
94calls or some variant of the
95.IR send (2)
96and
97.IR recv (2)
98calls. When a session has been completed a
99.IR close (2)
100may be performed.
101Out-of-band data may also be transmitted as described in
102.IR send (2)
103and received as described in
104.IR recv (2).
105.PP
106The communications protocols used to implement a
107SOCK_STREAM insure that data
108is not lost or duplicated. If a piece of data for which the
109peer protocol has buffer space cannot be successfully transmitted
110within a reasonable length of time, then
111the connection is considered broken and calls
112will indicate an error with
113\-1 returns and with ETIMEDOUT as the specific code
114in the global variable errno.
115The protocols optionally keep sockets \*(lqwarm\*(rq by
116forcing transmissions
117roughly every minute in the absence of other activity.
118An error is then indicated if no response can be
119elicited on an otherwise
120idle connection for a extended period (e.g. 5 minutes).
121A SIGPIPE signal is raised if a process sends
122on a broken stream; this causes naive processes,
123which do not handle the signal, to exit.
124.PP
125SOCK_DGRAM and SOCK_RAW
126sockets allow sending of datagrams to correspondents
127named in
128.IR send (2)
129calls. It is also possible to receive datagrams at
130such a socket with
131.IR recv (2).
132.PP
133An
134.IR fcntl (2)
135call can be used to specify a process group to receive
136a SIGURG signal when the out-of-band data arrives.
137.PP
138The operation of sockets is controlled by socket level
139.IR options .
140These options are defined in the file
141.RI < sys/socket.h >
142and explained below.
143.I Setsockopt
144and
145.IR getsockopt (2)
146are used to set and get options, respectively.
af71be09
KM
147Options other than SO_LINGER take an
148.I int
149parameter which should non-zero if the option is to be
150enabled, or zero if it is to be disabled; SO_LINGER
151uses a
152.I struct linger
153parameter, defined in
154.RI < sys/socket.h >,
155which specifies the desired state of the option and the
156linger interval (see below).
39582001
KM
157.PP
158.RS
159.DT
160.nf
161SO_DEBUG turn on recording of debugging information
162SO_REUSEADDR allow local address reuse
163SO_KEEPALIVE keep connections alive
af71be09
KM
164SO_DONTROUTE do not route outgoing messages
165SO_LINGER linger on close if data present
166SO_BROADCAST permit transmission of broadcast messages
39582001
KM
167.fi
168.RE
169.PP
170SO_DEBUG enables debugging in the underlying protocol modules.
171SO_REUSEADDR indicates the rules used in validating addresses supplied
172in a
173.IR bind (2)
174call should allow reuse of local addresses. SO_KEEPALIVE enables the
175periodic transmission of messages on a connected socket. Should the
176connected party fail to respond to these messages, the connection is
177considered broken and processes using the socket are notified via a
178SIGPIPE signal. SO_DONTROUTE indicates that outgoing messages should
179bypass the standard routing facilities. Instead, messages are directed
180to the appropriate network interface according to the network portion
181of the destination address. SO_LINGER
af71be09 182controls the action taken when unsent messags
39582001
KM
183are queued on socket and a
184.IR close (2)
185is performed.
186If the socket promises reliable delivery of data and SO_LINGER is set,
187the system will block the process on the
188.I close
189attempt until it is able to transmit the data or until it decides it
190is unable to deliver the information (a timeout period, termed the
191linger interval, is specified in the
192.IR setsockopt
193call when SO_LINGER is requested).
af71be09 194If SO_LINGER is disabled and a
39582001
KM
195.I close
196is issued, the system will process the close in a manner which allows
197the process to continue as quickly as possible.
198.SH "RETURN VALUE
199A \-1 is returned if an error occurs, otherwise the return
200value is a descriptor referencing the socket.
201.SH "ERRORS
202The \fIsocket\fP call fails if:
203.TP 20
204[EAFNOSUPPORT]
205The specified address family is not supported in this version
206of the system.
207.TP 20
208[ESOCKTNOSUPPORT]
209The specified socket type is not supported in this address family.
210.TP 20
211[EPROTONOSUPPORT]
212The specified protocol is not supported.
213.TP 20
214[EMFILE]
215The per-process descriptor table is full.
216.TP 20
217[ENOBUFS]
218No buffer space is available. The socket cannot be created.
219.SH SEE ALSO
220accept(2), bind(2), connect(2), getsockname(2), getsockopt(2),
221ioctl(2), listen(2), recv(2),
222select(2), send(2), shutdown(2), socketpair(2)
223.br
224``A 4.2BSD Interprocess Communication Primer''.
225.SH BUGS
226The use of keepalives is a questionable feature for this layer.