Berkeley Sockets Interface, or simply sockets, is a standardized set of communication functions launched by UC Berkeley in the early 1980s for their Berkeley Software Distribution (BSD). 30 years after its launch, this programming interface is offered in almost all popular programming languages (Java, C#, C++, …) .

The concept on which this interface is built are the sockets by which an application can connect to a network and thus communicate with another application connected from another computer.


A socket is a socket through which an application can send and receive data. This plug allows the app to connect to a network and communicate with other apps that are connected to it. Information written on a socket from one machine is read on the plug of another machine, and vice versa. There are different catch models, depending on network protocols TCP/IP4 sockets are the most common. The first programming interface (API for application programming interface) implementing the sockets was developed by the University of Berkeley for their Unix, in the 1980s. It is one of the first open source products in history.

The socket function of this API is used to create a certain type of socket. The type of socket will be chosen based on the communication technology to be used (for example TCP/IP). The API allows server software to serve multiple customers simultaneously.

A connection is made between the client and the server in order to allow communication. The connect function allows a customer to request the connection to a server, and the accept function allows a server to accept that connection. The server program will use the listen function beforehand to inform the underlying software that it is ready to receive connections. A closed function allows you to complete the connection. When one of the two interlocutors completes the connection, the other is immediately notified.

Once the connection is established, the send and recv functions are used to send and receive information, respectively. A gethostbyname auxiliary function allows you to obtain a machine’s IP address by querying the DNS, an address that will be used by other API functions.

Each socket has one type and one or more processes associated with it. It is also characterized by the field of communication in which it is located. The latter is an abstraction that allows to group processes with common properties and communicating through sockets. Normally, a socket can only exchange data with a socket in the same communication area.

4.3BSD’s inter-process communication supported three areas of communication:

  • The Internet domain for processe that use TCP/Ip protocol to communicate with each other.
  • The Unix domain in wich two processes on the same Unix station can only comunicate.
  • NS domain for processes that exchange data using Xerox’s standard protocol.

Types of sockets

The different types of sockets depend on a few properties visible to the programmer. There is nothing to prevent two sockets of different types from communicating with each other if the protocol used supports it — even though the processes are supposed to communicate only by sockets of the same type.

There are usually four types of sockets:

  • A socket stream allows for two-way, secure, sequened communication and a seamless data flow that can lead to fragmentation of the transmitted packages. In the Internet, this is the TCP protocol.
  • A datagram socket allows for two-way communication that is not secure, unsafe, and can eventually lead to data duplication. A process using this type of socket can therefore receive the data in a different order from the starting order. In the Internet, this is the UDP protocol.
  • A raw socket allows access to the raw content of data packets. Raw sockets are not intended for common users — only the root user can access them on most UNIX systems — and are used, for example, to analyze network traffic.
  • A socket sequenced packet, which looks like a socket stream except that it does not use packet fragmentations.

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.
  • 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.

Ahmed Alidjinou


Laisser un commentaire