Authors Top

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

Sockets are network resources that enable different processes to communicate with each other. In particular, an application using a socket has multiple operations to manage its lifecycle. Examples of these operations are bind, listen, send, receive, and close.

In this article, we’ll take a look at the main concepts of socket binding operation and understand how it works.

2. Socket Basics

A socket is an endpoint that enables communication between two processes. This communication is irrespective of where the processes are running. Furthermore, we highlight that sockets in the context of TCP/IP-based networks are called a Network Socket or Internet Socket. In this case, a socket executes between transport and application layers of the TCP/IP stack.

There are three main types of sockets:

  • Datagram: a socket that creates the interprocess communication with User Datagram Protocol (UDP). In this way, datagram sockets enable simple IP communication without establishing any connection between processes.
  • Stream: sockets that enable the communication between processes with Transmission Control Protocol (TCP). Thus, stream sockets require the execution of a three-way handshake to connect processes and provide communication with particular message delivering guarantees.
  • Raw: sockets that read network traffic from a network interface regardless of the transmission layer protocol. A typical use of raw sockets is on the development of packet sniffers.

The type defines properties related to the communication of sockets. In this way, the choice for a particular type may change how some sockets’ lifecycle operations work. Examples of these operations are bind (detailed discussed in the next section), close, and shutdown.

Finally, it is important to notice that, besides defining the type, opening a socket also requires the specification of a protocol family. These protocol families are determinant factors to establish a socket address, which influences the binding operation. We’ll see more details about protocol families in the next section.

3. Socket Binding

After opened, a socket is created but does not have any address assigned. So, the bind operation is responsible for establishing specific addresses to sockets.

Before executing the bind operation, we should select the proper address according to the protocol family defined for a socket while opening it. Next, we cite the most common socket protocol families and present the data that compose their addresses.

  • AF_INET: protocol family referring to the Internet Protocol Version 4 (IPv4). Thus, sockets opened with this protocol family are bound with an IPv4 address and a port number.
  • AF_INET6: protocol family referring to the Internet Protocol Version 6 (IPv6). A socket address, in this case, is similar to the AF_INET address. So, it is also composed of an IP and a port number, but with an IP in version 6 format.
  • AF_LOCAL: protocol family that enables local communication between processes. This protocol family does not execute over IP. Thus, sockets created with AF_LOCAL bind to addresses consisting of null-terminated file pathnames.
  • AF_PACKET: protocol family employed for working with raw packets directly at the device level (OSI Layer 2 or TCP/IP Layer 1). In such a way, the address is typically the network interface from where the socket should catch the network traffic.

After defined the protocol family of a socket, we are able to bind it with an address. We illustrate the execution of bind operations for the previously presented protocol families in the following image:

3.1. Some Aspects of Binding Usage

In practical terms, several operational aspects from the deployment scenario should be considered before binding a socket. For example, in the server/client model, the binding operation is, in most cases, explicitly executed only on the server-side. Once the server waits for communication, we as clients expect that it responds in a static and knowable address. In contrast, the server receives the client addresses when they start the communication. So, it is not necessary to execute an explicit bind to clients.

We can cite an exception for the previously presented scenario: servers that respond to clients communicating from particular addresses. In that case, clients must execute an explicit bind too.

Finally, there are some addresses with special characteristics for binding a socket. For example, an AF_INET socket bound to IP or an AF_INET6 socket bound to ::/0 IP will typically listen to every network interface available. Furthermore, the operating system will dynamically define a suitable port for Network Sockets bound in port 0.

4. Conclusion

In this article, we took a look at the socket binding operation. First, we reviewed the basic concepts of sockets. Then, we studied the requirements and characteristics of socket binding.

Since it establishes addresses to sockets, we can conclude that the bind operation is crucial for enabling interprocess communication.

Authors Bottom

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!