---
name: "socket"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/socket/2"
---

# socket(2)

create an endpoint for communication

## NAME

**socket** – create an endpoint for communication

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/socket.h\>**

*int*\

**socket**(*int domain*, *int type*, *int protocol*);

## DESCRIPTION

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. See [unix(4)](/unix/4.md).

- PF\_INET

  Internet version 4 protocols. See [icmp(4)](/icmp/4.md), [igmp(4)](/igmp/4.md), [ip(4)](/ip/4.md), [sctp(4)](/sctp/4.md), [tcp(4)](/tcp/4.md), [udp(4)](/udp/4.md), [udplite(4)](/udplite/4.md).

- PF\_INET6

  Internet version 6 protocols. See [icmp6(4)](/icmp6/4.md), [ip6(4)](/ip6/4.md), [mld(4)](/mld/4.md).

- PF\_DIVERT

  Firewall packet diversion/re-injection. See [divert(4)](/divert/4.md).

- PF\_ROUTE

  Legacy protocol to control routing tables and receive network configuration events from the kernel. New applications should prefer [rtnetlink(4)](/rtnetlink/4.md) over [route(4)](/route/4.md).

- PF\_KEY

  Internal key-management function. See [ipsec(4)](/ipsec/4.md).

- PF\_NETGRAPH

  Netgraph sockets. See [netgraph(3)](/netgraph/3.md) and [ng\_socket(4)](/ng_socket/4.md).

- PF\_NETLINK

  Netlink protocols. See [genetlink(4)](/genetlink/4.md), [netlink(4)](/netlink/4.md), [rtnetlink(4)](/rtnetlink/4.md).

- PF\_BLUETOOTH

  Bluetooth protocols. See [ng\_btsocket(4)](/ng_btsocket/4.md).

- PF\_INET\_SDP

  OFED socket direct protocol (IPv4).

- PF\_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 socket. See [fcntl(2)](/fcntl/2.md) FD\_CLOEXEC flag for F\_GETFD command.

- SOCK\_CLOFORK

  Set close-on-fork on the new socket. See [fcntl(2)](/fcntl/2.md) FD\_CLOFORK flag for F\_GETFD command.

- SOCK\_NONBLOCK

  Set non-blocking mode on the new socket. See [fcntl(2)](/fcntl/2.md) O\_NONBLOCK flag for F\_SETFL command.

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)](/protocols/5.md). The *protocol* argument may be set to zero (0) to request the default implementation of a socket type for the protocol, if any.

## STREAM SOCKET TYPE

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)](/connect/2.md) 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)](/sendto/2.md) system call.) Once connected, data may be sent using [send(2)](/send/2.md), [sendto(2)](/sendto/2.md), [sendmsg(2)](/sendmsg/2.md) and [write(2)](/write/2.md) system calls. Data may be received using [recv(2)](/recv/2.md), [recvfrom(2)](/recvfrom/2.md), [recvmsg(2)](/recvmsg/2.md), and [read(2)](/read/2.md) 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)](/send/2.md) and received as described in [recv(2)](/recv/2.md).

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)](/send/2.md), [sendto(2)](/sendto/2.md), and [sendmsg(2)](/sendmsg/2.md) system calls or by the SO\_NOSIGPIPE socket option set on the socket with [setsockopt(2)](/setsockopt/2.md).

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.

## DATAGRAM SOCKET TYPE

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)](/recvfrom/2.md) or [recvmsg(2)](/recvmsg/2.md). An application can also pre-specify a peer address with [sendto(2)](/sendto/2.md) or [sendmsg(2)](/sendmsg/2.md), 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.

## SEQUENCED PACKET SOCKET TYPE

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)](/send/2.md) or [sendmsg(2)](/sendmsg/2.md) functions. There is no possibility to set a record boundary with [write(2)](/write/2.md). Record boundaries are visible to the receiver via the MSG\_EOR flag in the received message flags returned by the [recvmsg(2)](/recvmsg/2.md) 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.

## RAW SOCKET TYPE

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)](/ip/4.md) and [ip6(4)](/ip6/4.md).

## NON-BLOCKING MODE

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)](/fcntl/2.md) 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)](/recv/2.md), [recvfrom(2)](/recvfrom/2.md), [recvmsg(2)](/recvmsg/2.md) and [read(2)](/read/2.md) 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)](/send/2.md), [sendto(2)](/sendto/2.md), [sendmsg(2)](/sendmsg/2.md) or [write(2)](/write/2.md) 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.

## OTHER OPERATIONS ON SOCKETS

Since socket descriptors are file descriptors, many generic file operations performed by [fcntl(2)](/fcntl/2.md), apply. Socket descriptors can be used with all event engines, such as [kevent(2)](/kevent/2.md), [select(2)](/select/2.md) and [poll(2)](/poll/2.md).

An [fcntl(2)](/fcntl/2.md) 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)](/setsockopt/2.md) and [getsockopt(2)](/getsockopt/2.md) system calls are used to set and get options, respectively.

Connection associated with a socket can be terminated by [close(2)](/close/2.md) system call. One direction of communication can be disabled with [shutdown(2)](/shutdown/2.md).

## RETURN VALUES

A -1 is returned if an error occurs, otherwise the return value is a descriptor referencing the socket.

## ERRORS

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.

## SEE ALSO

[accept(2)](/accept/2.md), [bind(2)](/bind/2.md), [close(2)](/close/2.md), [connect(2)](/connect/2.md), [fcntl(2)](/fcntl/2.md), [getpeername(2)](/getpeername/2.md), [getsockname(2)](/getsockname/2.md), [getsockopt(2)](/getsockopt/2.md), [ioctl(2)](/ioctl/2.md), [kevent(2)](/kevent/2.md), [listen(2)](/listen/2.md), [poll(2)](/poll/2.md), [read(2)](/read/2.md), [recv(2)](/recv/2.md), [select(2)](/select/2.md), [send(2)](/send/2.md), [sendmsg(2)](/sendmsg/2.md), [sendto(2)](/sendto/2.md), [shutdown(2)](/shutdown/2.md), [socketpair(2)](/socketpair/2.md), [write(2)](/write/2.md), [CMSG\_DATA(3)](/CMSG_DATA/3.md), [getprotoent(3)](/getprotoent/3.md), [netgraph(3)](/netgraph/3.md), [signal(3)](/signal/3.md), [divert(4)](/divert/4.md), [genetlink(4)](/genetlink/4.md), [icmp(4)](/icmp/4.md), [icmp6(4)](/icmp6/4.md), [igmp(4)](/igmp/4.md), [ip(4)](/ip/4.md), [ip6(4)](/ip6/4.md), [ipsec(4)](/ipsec/4.md), [netintro(4)](/netintro/4.md), [netlink(4)](/netlink/4.md), [ng\_socket(4)](/ng_socket/4.md), [route(4)](/route/4.md), [rtnetlink(4)](/rtnetlink/4.md), [sctp(4)](/sctp/4.md), [tcp(4)](/tcp/4.md), [udp(4)](/udp/4.md), [protocols(5)](/protocols/5.md) “An Introductory 4.3 BSD Interprocess Communication Tutorial”, *PS1*, 7. “BSD Interprocess Communication Tutorial”, *PS1*, 8.

## STANDARDS

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.

## HISTORY

The **socket**() system call appeared in 4.2BSD.

The SOCK\_CLOFORK flag appeared in FreeBSD 15.0.
