A socket represents an endpoint of a communications channel. The socket API places few limitations on the nature of this channel, and this has made it possible for many different types of computer network to be accessed through a single set of library functions.

When a socket is created it is given a domain, which defines the type of address (and hence the type of network) that the socket can be bound to. Examples include AF_INET for IPv4, AF_INET6 for IPv6, or AF_UNIX for a UNIX-domain socket (used for inter-process communication on the local machine). Sockets also have a type, typically SOCK_STREAM (for connection-oriented protocols like TCP that carry a stream of bytes) or SOCK_DGRAM (for protocols like UDP that provide a datagram service).

Once created, a stream socket can be instructed either to initiate an outbound connection (using the connect function) or to wait and listen for inbound connections (using the bind, listen and connect functions). Datagram sockets can be used immediately without establishing a connection.


Further reading