socket(3)

NAME
socket - create an endpoint for communication

SYNOPSIS
#include <sys/socket.h>

int socket(int af, int type, int protocol);

DESCRIPTION
socket creates an endpoint for communication and returns a descriptor. The socket descriptor returned is used in all subsequent socket-related library calls.

The af parameter specifies an address family to be used to interpret addresses in later operations that specify the socket. These address families are defined in the include file <sys/socket.h> The only currently-supported address family is

    AF_UNIX (path names on a local host)

The type specifies the semantics of communication for the socket. Currently defined types are:

    SOCK_STREAM
    SOCK_DGRAM

A SOCK_STREAM type provides sequenced, reliable, two-way-connection-based byte streams. A SOCK_DGRAM socket supports datagrams (connectionless, unreliable messages of a fixed, typically small, maximum length).

protocol specifies a particular protocol to be used with the socket. Normally, only a single protocol exists to support a particular socket type using a given address family. However, many protocols may exist, in which case a particular protocol must be specified. The protocol number to use depends on the communication domain in which communication is to take place. protocol can be supplied as zero, in which case the system chooses a protocol type to use.

Sockets of type SOCK_STREAM are byte streams similar to pipes except that they are full-duplex instead of half-duplex. A stream socket must be in a connected state before any data can be sent or received on it. A connection to another socket is created with a connect(3) or accept(3) call. Once connected, data can be transferred using some variant of the send(3) and recv(3) or the read(3) and write(3) calls. When a session has been completed, a close(3) can be performed.

A SIGPIPE signal is raised if a process sends on a broken stream; this causes naive processes that do not handle the signal to exit. An end-of-file condition (zero bytes read) is returned if a process tries to read on a broken stream.

SOCK_DGRAM sockets allow sending of messages to correspondents named in send(3) calls. It is also possible to receive messages at such a socket with recv(3).

The operation of sockets is controlled by socket level options set by the setsockopt library call described by the getsockopt(3) reference page. These options are defined in the file <sys/socket.h> and explained in the getsockopt (3) reference page.

RETURN VALUE
If the call is successful, a valid file descriptor referencing the socket is returned. If it fails, -1 is returned and an error code is stored in errno.

DIAGNOSTICS
socket fails if:

[ENOENT]
    The socket device driver has not been started.

[EAFNOSUPPORT]
    The specified address family is not supported.

[ESOCKTNOSUPPORT]
    The specified socket type is not supported in this address family.

[EPROTONOSUPPORT]
    The specified protocol is not supported.

[EMFILE]
    The per-process descriptor table is full.

[ENOMEM]
    No buffer space is available. The socket cannot be created.

[EPROTOTYPE]
    The type of socket and protocol do not match.

SEE ALSO
accept(3), bind(3), connect(3), getsockname(3), getsockopt(3), ioctl(3), listen(3), recv(3), select(3), send(3), shutdown(3).