socket —
create an endpoint for
communication
Standard C Library (libc, -lc)
#include
<sys/socket.h>
int
socket(int
domain, int type,
int protocol);
The
socket()
system call creates an endpoint for communication and returns a
descriptor.
The domain argument specifies a
communications domain within which communication will take place; this
selects the protocol family which should be used. These families are defined
in the include file
<sys/socket.h>. The
currently understood formats are:
PF_LOCAL Host-internal protocols (alias for PF_UNIX),
PF_UNIX Host-internal protocols,
PF_INET Internet version 4 protocols,
PF_INET6 Internet version 6 protocols,
PF_DIVERT Firewall packet diversion/re-injection,
PF_ROUTE Internal routing protocol,
PF_KEY Internal key-management function,
PF_NETGRAPH Netgraph sockets,
PF_NETLINK Netlink protocols,
PF_BLUETOOTH Bluetooth protocols,
PF_INET_SDP OFED socket direct protocol (IPv4),
AF_HYPERV HyperV sockets
Each protocol family is connected to an address family, which has
the same name except that the prefix is
“AF_” in place of
“PF_”. Other protocol families may be
also defined, beginning with “PF_”,
with corresponding address families.
The socket has the indicated type, which
specifies the semantics of communication. Currently defined types are:
SOCK_STREAM Stream socket,
SOCK_DGRAM Datagram socket,
SOCK_RAW Raw-protocol interface,
SOCK_SEQPACKET Sequenced packet stream
Additionally, the following flags are allowed in the
type argument:
SOCK_CLOEXEC Set close-on-exec on the new descriptor,
SOCK_CLOFORK Set close-on-fork on the new descriptor,
SOCK_NONBLOCK Set non-blocking mode on the new socket
The protocol argument 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 “communication domain” in which
communication is to take place; see
protocols(5). The protocol argument may
be set to zero (0) to request the default implementation of a socket type
for the protocol, if any.
The SOCK_STREAM socket type provides
reliable, sequenced, full-duplex octet streams between the socket and a peer
to which the socket is connected. A socket of type
SOCK_STREAM needs to be in a
connected
state before any data can be sent or received. A connection to another
socket is created with a
connect(2) system call. (Some protocol families, such as the
Internet family, support the notion of an “implied connect”,
which permits data to be sent piggybacked onto a connect operation by using
the
sendto(2) system call.) Once connected, data may be sent
using
send(2),
sendto(2),
sendmsg(2) and
write(2) system calls. Data may be received using
recv(2),
recvfrom(2),
recvmsg(2), and
read(2) system calls. Record boundaries are not maintained;
data sent on a stream socket using output operations of one size can be
received using input operations of smaller or larger sizes without loss of
data. Data may be buffered; successful return from an output function does
not imply that the data has been delivered to the peer or even transmitted
from the local system. For certain protocols out-of-band data may also be
transmitted as described in
send(2) and received as described in
recv(2).
If data cannot be successfully transmitted within a given time
then the connection is considered broken, and subsequent operations shall
fail with a protocol specific error code. A SIGPIPE
signal is raised if a thread attempts to send data on a broken stream (one
that is no longer connected). The signal can be suppressed by the
MSG_NOSIGNAL flag with distinct
send(2),
sendto(2), and
sendmsg(2) system calls or by the
SO_NOSIGPIPE socket option set on the socket with
setsockopt(2).
The SOCK_STREAM socket is supported by the
following protocol families: PF_INET,
PF_INET6, PF_UNIX,
PF_BLUETOOTH, PF_HYPERV, and
PF_INET_SDP. Out-of-band data transmission mechanism
is supported for stream sockets of PF_INET and
PF_INET6 protocol families.
The SOCK_DGRAM socket type supports
connectionless data transfer which is not necessarily acknowledged or
reliable. Datagrams can be sent to the address specified (possibly multicast
or broadcast) in each output operation, and incoming datagrams can be
received from multiple sources. The source address of each datagram is
available when receiving the datagram with
recvfrom(2) or
recvmsg(2). An application can also pre-specify a peer
address with
sendto(2) or
sendmsg(2), in which case calls to output functions that do
not specify a peer address shall send to the pre-specified peer. If a peer
has been specified, only datagrams from that peer shall be received. A
datagram shall be sent in a single output operation, and needs to be
received in a single input operation. The maximum size of a datagram is
protocol-specific. Output datagrams may be buffered within the system; thus,
a successful return from an output function does not guarantee that a
datagram is actually sent or received.
The SOCK_DGRAM socket is supported by the
following protocol families: PF_INET,
PF_INET6, PF_UNIX,
PF_NETGRAPH, and
PF_NETLINK.
The SOCK_SEQPACKET socket type is similar
to the SOCK_STREAM type, and is also
connection-oriented. The only difference between these types is that record
boundaries are maintained using the SOCK_SEQPACKET
type. A record can be sent using one or more output operations and received
using one or more input operations, but a single operation never transfers
parts of more than one record. Record boundaries are set by the sender with
the MSG_EOR flag of
send(2) or
sendmsg(2) functions. There is no possibility to set a record
boundary with
write(2). Record boundaries are visible to the receiver via
the MSG_EOR flag in the received message flags
returned by the
recvmsg(2) function. It is protocol-specific whether a
maximum record size is imposed.
The SOCK_SEQPACKET socket is supported by
the following protocol families: PF_INET,
PF_INET6, and PF_UNIX.
The SOCK_RAW socket type provides access
to internal network protocols and interfaces. It is a datagram socket in its
nature, thus has the same semantics of read and write operations. The
SOCK_RAW type is available only to the super-user
and is described in
ip(4) and
ip6(4).
A socket can be created in
non-blocking
mode with the help of SOCK_NONBLOCK flag.
Alternatively, the non-blocking mode on a socket can be turned on and off
with the help of the O_NONBLOCK flag of the
fcntl(2) system call.
When a non-blocking socket has not enough data in its receive
buffer to fulfill the application supplied buffer, then data receiving
system calls like
recv(2),
recvfrom(2),
recvmsg(2) and
read(2) will not block waiting for the data but immediately
return. Return value will indicate amount of bytes read into the supplied
buffer. The errno will be set to
EAGAIN (has same value as
EWOULDBLOCK).
If application tries to send more data on a non-blocking socket
than the socket send buffer can accomodate with
send(2),
sendto(2),
sendmsg(2) or
write(2) system calls partial data will be sent. Return value
will indicate amount of bytes sent. The errno will be
set to EAGAIN. Note that sockets of
SOCK_DGRAM type are unreliable, thus for these
sockets sending operations will never fail with
EAGAIN in non-blocking mode neither will block in
blocking mode.
Since socket descriptors are file descriptors, many generic file
operations performed by
fcntl(2), apply. Socket descriptors can be used with all
event engines, such as
kevent(2),
select(2) and
poll(2).
An
fcntl(2) system call can be used to specify a process group
to receive a SIGURG signal when the out-of-band data
arrives. It may also enable non-blocking I/O and asynchronous notification
of I/O events via SIGIO.
The operation of sockets is controlled by socket
level options.
These options are defined in the file
<sys/socket.h>. The
setsockopt(2) and
getsockopt(2) system calls are used to set and get options,
respectively.
Connection associated with a socket can be terminated by
close(2) system call. One direction of communication can be
disabled with
shutdown(2).
A -1 is returned if an error occurs, otherwise the return value is
a descriptor referencing the socket.
The socket() system call fails if:
- [
EACCES]
- Permission to create a socket of the specified type and/or protocol is
denied.
- [
EAFNOSUPPORT]
- The address family (domain) is not supported or the specified domain is
not supported by this protocol family.
- [
EMFILE]
- The per-process descriptor table is full.
- [
ENFILE]
- The system file table is full.
- [
ENOBUFS]
- Insufficient buffer space is available. The socket cannot be created until
sufficient resources are freed.
- [
EPERM]
- User has insufficient privileges to carry out the requested
operation.
- [
EPROTONOSUPPORT]
- The protocol type or the specified protocol is not supported within this
domain.
- [
EPROTOTYPE]
- The socket type is not supported by the protocol.
accept(2),
bind(2),
close(2),
connect(2),
fcntl(2),
getpeername(2),
getsockname(2),
getsockopt(2),
ioctl(2),
kevent(2),
listen(2),
poll(2),
read(2),
recv(2),
select(2),
send(2),
sendmsg(2),
sendto(2),
signal(3),
shutdown(2),
socketpair(2),
write(2),
CMSG_DATA(3),
getprotoent(3),
divert(4),
ip(4),
ip6(4),
netgraph(4),
protocols(5)
“An Introductory 4.3 BSD
Interprocess Communication Tutorial”,
PS1, 7.
“BSD Interprocess
Communication Tutorial”, PS1,
8.
The socket() function conforms to
IEEE Std 1003.1-2008 (“POSIX.1”). The
POSIX standard specifies only the AF_INET,
AF_INET6, and AF_UNIX
constants for address families, and requires the use of
AF_* constants for the domain
argument of socket(). The
SOCK_CLOEXEC and
SOCK_CLOFORK flags are expected to conform to
IEEE Std 1003.1-2024 (“POSIX.1”).
POSIX standard. The SOCK_RDM
type, the PF_* constants, and
other address families are FreeBSD extensions.
The socket() system call appeared in
4.2BSD.
The SOCK_CLOFORK flag appeared in
FreeBSD 15.0.