Berkeley sockets is an application programming interface (API) for Internet domain sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules. It originated with the 4.2BSD Unix operating system, which was released in 1983. A socket is an abstract representation (handle) for the local endpoint of a network communication path. The Berkeley sockets API represents it as a file descriptor in the Unix philosophy that provides a common interface for input and output to streams of data. Berkeley sockets evolved with little modification from a de facto standard into a component of the POSIX specification. The term POSIX sockets is essentially synonymous with Berkeley sockets, but they are also known as BSD sockets, acknowledging the first implementation in the Berkeley Software Distribution.
History and implementations Berkeley sockets originated with the 4.2BSD Unix operating system, released in 1983, as a programming interface. Not until 1989, however, could the University of California, Berkeley release versions of the operating system and networking library free from the licensing constraints of AT&T Corporation's proprietary Unix. All modern operating systems implement a version of the Berkeley socket interface. It became the standard interface for applications running in the Internet. Even the Winsock implementation for Microsoft Windows, created by unaffiliated developers, closely follows the standard. The BSD sockets API is written in the C programming language. Most other programming languages provide similar interfaces, typically written as a wrapper library based on the C API.
BSD and POSIX sockets As the Berkeley socket API evolved and ultimately yielded the POSIX socket API, certain functions were deprecated or removed and replaced by others. The POSIX API is also designed to be reentrant and supports IPv6.
Alternatives The STREAMS-based Transport Layer Interface (TLI) API offers an alternative to the socket API. Many systems that provide the TLI API also provide the Berkeley socket API. Non-Unix systems often expose the Berkeley socket API with a translation layer to a native networking API. Plan 9 and Genode use file-system APIs with control files rather than file-descriptors.
Header files The Berkeley socket interface is defined in several header files. The names and content of these files differ slightly between implementations. In general, they include:
Socket API functions
The Berkeley socket API typically provides the following functions:
socket() creates a new socket of a certain type, identified by an integer number, and allocates system resources to it. bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local IP address and a port number. listen() is used on the server side, and causes a bound TCP socket to enter listening state. connect() is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to establish a new TCP connection. accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection. send(), recv(), sendto(), and recvfrom() are used for sending and receiving data. The standard functions write() and read() may also be used. close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated. gethostbyname() and gethostbyaddr() are used to resolve host names and addresses. IPv4 only. getaddrinfo() and freeaddrinfo() are used to resolve host names and addresses. IPv4, IPv6. select() is used to suspend, waiting for one or more of a provided list of sockets to be ready to read, ready to write, or that have errors. poll() is used to check on the state of a socket in a set of sockets. The set can be tested to see if any socket can be written to, read from or if an error occurred. getsockopt() is used to retrieve the current value of a particular socket option for the specified socket. setsockopt() is used to set a particular socket option for the specified socket.
socket The function socket() creates an endpoint for communication and returns a file descriptor for the socket. It uses three arguments:
domain, which specifies the protocol family of the created socket. For example: PF_INET for network protocol IPv4 (IPv4-only) PF_INET6 for IPv6 (and in some cases, backward compatible with IPv4) PF_UNIX for local socket (using a special filesystem node) type, one of: SOCK_STREAM (reliable stream-oriented service or stream sockets) SOCK_DGRAM (datagram service or datagram sockets) SOCK_SEQPACKET (reliable sequenced packet service) SOCK_RAW (raw protocols atop the network layer) protocol specifying the actual transport protocol to use. The most common are IPPROTO_TCP, IPPROTO_SCTP, IPPROTO_UDP, IPPROTO_DCCP. These protocols are specified in file <netinet/in.h>. The value 0 may be used to select a default protocol from the selected domain and type. The function returns -1 if an error occurred. Otherwise, it returns an integer representing the newly assigned descriptor.
bind bind() associates a socket with an address. When a socket is created with socket(), it is only given a protocol family, but not assigned an address. This association must be performed before the socket can accept connections from other hosts. The function has three arguments:
sockfd, a descriptor representing the socket my_addr, a pointer to a sockaddr structure representing the address to bind to. addrlen, a field of type socklen_t specifying the size of the sockaddr structure. bind() returns 0 on success and -1 if an error occurs.
listen After a socket has been associated with an address, listen() prepares it for incoming connections. However, this is only necessary for the stream-oriented (connection-oriented) data modes, i.e., for socket types (SOCK_STREAM, SOCK_SEQPACKET). listen() requires two arguments:
sockfd, a valid socket descriptor. backlog, an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value. Once a connection is accepted, it is dequeued. On success, 0 is returned. If an error occurs, -1 is returned.
… excerpt ends here. Continue reading the full article.
