.\" Copyright (c) 1983, 1991 The Regents of the University of California.
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" @(#)socket.2 6.8 (Berkeley) 3/10/91
.Nd create an endpoint for communication
.Fd #include <sys/types.h>
.Fd #include <sys/socket.h>
.Fn socket "int domain" "int type" "int protocol"
creates an endpoint for communication and returns a descriptor.
parameter specifies a communications domain within which
communication will take place; this selects the protocol family
These families are defined in the include file
The currently understood formats are
.Bd -literal -offset indent -compact
AF_UNIX (UNIX internal protocols),
AF_INET (ARPA Internet protocols),
AF_NS (Xerox Network Systems protocols), and
AF_IMPLINK (IMP \*(lqhost at IMP\*(rq link layer).
The socket has the indicated
which specifies the semantics of communication. Currently
.Bd -literal -offset indent -compact
type provides sequenced, reliable,
two-way connection based byte streams.
An out-of-band data transmission mechanism may be supported.
datagrams (connectionless, unreliable messages of
a fixed (typically small) maximum length).
socket may provide a sequenced, reliable,
two-way connection-based data transmission path for datagrams
of fixed maximum length; a consumer may be required to read
an entire packet with each read system call.
This facility is protocol specific, and presently implemented
sockets provide access to internal network protocols and interfaces.
which is available only to the super-user, and
but not yet implemented, are not described here.
specifies a particular protocol to be used with the socket.
Normally only a single protocol exists to support a particular
socket type within a given protocol family. However, it is possible
that many protocols may exist, in which case a particular protocol
must be specified in this manner. The protocol number to use is
particular to the \*(lqcommunication domain\*(rq in which communication
are full-duplex byte streams, similar
to pipes. A stream socket must be in a
state before any data may be sent or received
on it. A connection to another socket is created with a
call. Once connected, data may be transferred using
calls or some variant of the
calls. When a session has been completed a
Out-of-band data may also be transmitted as described in
and received as described in
The communications protocols used to implement a
is not lost or duplicated. If a piece of data for which the
peer protocol has buffer space cannot be successfully transmitted
within a reasonable length of time, then
the connection is considered broken and calls
will indicate an error with
The protocols optionally keep sockets
roughly every minute in the absence of other activity.
An error is then indicated if no response can be
idle connection for a extended period (e.g. 5 minutes).
signal is raised if a process sends
on a broken stream; this causes naive processes,
which do not handle the signal, to exit.
sockets employ the same system calls
sockets. The only difference
calls will return only the amount of data requested,
and any remaining in the arriving packet will be discarded.
sockets allow sending of datagrams to correspondents
calls. Datagrams are generally received with
which returns the next datagram with its return address.
call can be used to specify a process group to receive
signal when the out-of-band data arrives.
It may also enable non-blocking I/O
and asynchronous notification of I/O events
The operation of sockets is controlled by socket level
These options are defined in the file
are used to set and get options, respectively.
A -1 is returned if an error occurs, otherwise the return
value is a descriptor referencing the socket.
.Bl -tag -width EPROTONOPSUPPORTA
.It Bq Er EPROTONOSUPPORT
The protocol type or the specified protocol is not supported
The per-process descriptor table is full.
The system file table is full.
Permission to create a socket of the specified type and/or protocol
Insufficient buffer space is available.
The socket cannot be created until sufficient resources are freed.
.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
.%T "BSD Interprocess Communication Tutorial"
.%O "reprinted in UNIX Programmer's Supplementary Documents Volume 1"
function call appeared in