update from Chris for ANSI _doprnt; add BUGS section mentioning
[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.\"
cb9fed07 5.\" @(#)socket.2 6.5 (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
d25f1c67
MK
18s = socket(domain, type, protocol)
19int s, domain, type, protocol;
39582001
KM
20.fi
21.SH DESCRIPTION
22.I Socket
23creates an endpoint for communication and returns a descriptor.
24.PP
25The
d25f1c67
MK
26.I domain
27parameter specifies a communications domain within which
28communication will take place; this selects the protocol family
29which should be used.
30The protocol family generally is the same as the address family
31for the addresses supplied in later operations on the socket.
32These families are defined in the include file
39582001
KM
33.IR <sys/socket.h> .
34The currently understood formats are
35.PP
36.RS
37.nf
38.ta 1.25i 1.75i
d25f1c67
MK
39PF_UNIX (UNIX internal protocols),
40PF_INET (ARPA Internet protocols),
41PF_NS (Xerox Network Systems protocols), and
42PF_IMPLINK (IMP \*(lqhost at IMP\*(rq link layer).
39582001
KM
43.fi
44.RE
45.PP
46The socket has the indicated
eff6446c 47.I type,
39582001
KM
48which specifies the semantics of communication. Currently
49defined types are:
50.PP
51.RS
52.nf
53SOCK_STREAM
54SOCK_DGRAM
55SOCK_RAW
56SOCK_SEQPACKET
57SOCK_RDM
58.fi
59.RE
60.PP
61A SOCK_STREAM type provides sequenced, reliable,
d25f1c67
MK
62two-way connection based byte streams.
63An out-of-band data transmission mechanism may be supported.
39582001
KM
64A SOCK_DGRAM socket supports
65datagrams (connectionless, unreliable messages of
66a fixed (typically small) maximum length).
cab13e85
KM
67A SOCK_SEQPACKET socket may provide a sequenced, reliable,
68two-way connection-based data transmission path for datagrams
69of fixed maximum length; a consumer may be required to read
70an entire packet with each read system call.
71This facility is protocol specific, and presently implemented
d25f1c67
MK
72only for PF_NS.
73SOCK_RAW sockets provide access to internal network protocols and interfaces.
39582001
KM
74The types SOCK_RAW,
75which is available only to the super-user, and
cab13e85 76SOCK_RDM, which is planned,
39582001
KM
77but not yet implemented, are not described here.
78.PP
79The
80.I protocol
81specifies a particular protocol to be used with the socket.
82Normally only a single protocol exists to support a particular
d25f1c67
MK
83socket type within a given protocol family. However, it is possible
84that many protocols may exist, in which case a particular protocol
39582001
KM
85must be specified in this manner. The protocol number to use is
86particular to the \*(lqcommunication domain\*(rq in which communication
87is to take place; see
39582001
KM
88.IR protocols (3N).
89.PP
90Sockets of type SOCK_STREAM
91are full-duplex byte streams, similar
92to pipes. A stream socket must be in a
93.I connected
94state before any data may be sent or received
95on it. A connection to another socket is created with a
96.IR connect (2)
97call. Once connected, data may be transferred using
98.IR read (2)
99and
100.IR write (2)
101calls or some variant of the
102.IR send (2)
103and
104.IR recv (2)
105calls. When a session has been completed a
106.IR close (2)
107may be performed.
108Out-of-band data may also be transmitted as described in
109.IR send (2)
110and received as described in
111.IR recv (2).
112.PP
113The communications protocols used to implement a
114SOCK_STREAM insure that data
115is not lost or duplicated. If a piece of data for which the
116peer protocol has buffer space cannot be successfully transmitted
117within a reasonable length of time, then
118the connection is considered broken and calls
119will indicate an error with
120\-1 returns and with ETIMEDOUT as the specific code
121in the global variable errno.
122The protocols optionally keep sockets \*(lqwarm\*(rq by
123forcing transmissions
124roughly every minute in the absence of other activity.
125An error is then indicated if no response can be
126elicited on an otherwise
127idle connection for a extended period (e.g. 5 minutes).
128A SIGPIPE signal is raised if a process sends
129on a broken stream; this causes naive processes,
130which do not handle the signal, to exit.
131.PP
cab13e85
KM
132SOCK_SEQPACKET sockets employ the same system calls
133as SOCK_STREAM sockets. The only difference
134is that
135.IR read (2)
136calls will return only the amount of data requested,
137and any remaining in the arriving packet will be discarded.
138.PP
39582001
KM
139SOCK_DGRAM and SOCK_RAW
140sockets allow sending of datagrams to correspondents
141named in
142.IR send (2)
d25f1c67
MK
143calls. Datagrams are generally received with
144.IR recvfrom (2),
145which returns the next datagram with its return address.
39582001
KM
146.PP
147An
148.IR fcntl (2)
149call can be used to specify a process group to receive
150a SIGURG signal when the out-of-band data arrives.
d25f1c67
MK
151It may also enable non-blocking I/O
152and asynchronous notification of I/O events
153via SIGIO.
39582001
KM
154.PP
155The operation of sockets is controlled by socket level
156.IR options .
157These options are defined in the file
d25f1c67
MK
158.RI < sys/socket.h >.
159.IR Setsockopt (2)
39582001
KM
160and
161.IR getsockopt (2)
162are used to set and get options, respectively.
39582001
KM
163.SH "RETURN VALUE
164A \-1 is returned if an error occurs, otherwise the return
165value is a descriptor referencing the socket.
166.SH "ERRORS
167The \fIsocket\fP call fails if:
168.TP 20
39582001 169[EPROTONOSUPPORT]
d25f1c67
MK
170The protocol type or the specified protocol is not supported
171within this domain.
39582001
KM
172.TP 20
173[EMFILE]
174The per-process descriptor table is full.
175.TP 20
d25f1c67
MK
176[ENFILE]
177The system file table is full.
178.TP 20
179[EACCESS]
180Permission to create a socket of the specified type and/or protocol
181is denied.
182.TP 20
39582001 183[ENOBUFS]
d25f1c67
MK
184Insufficient buffer space is available.
185The socket cannot be created until sufficient resources are freed.
39582001
KM
186.SH SEE ALSO
187accept(2), bind(2), connect(2), getsockname(2), getsockopt(2),
d25f1c67
MK
188ioctl(2), listen(2), read(2), recv(2),
189select(2), send(2), shutdown(2), socketpair(2), write(2)
39582001 190.br
cb9fed07
KD
191``An Introductory 4.3BSD Interprocess Communication Tutorial.''
192(reprinted in UNIX Programmer's Supplementary Documents Volume 1, PS1:7)
d25f1c67 193``An Advanced 4.3BSD Interprocess Communication Tutorial.''
cb9fed07 194(reprinted in UNIX Programmer's Supplementary Documents Volume 1, PS1:8)