Sockets introduction

Sockets are the basis of IP, but we can also use them to take advantage of it, that is, through sockets, we can make two applications communicate with each other. A socket in programming is a communication tunnel that helps two applications to communicate and are the basis of the internet and its protocols, such as HTTP, FTP, and SMTP.

This mechanism emerged in the early 80s with the Unix system at Berkeley, to provide a communication channel between processes and have the same functionality as communication by mail or telephone—that is, they allow a process to speak with another, even when they are in different machines. This interconnect feature makes the socket concept very useful.

For two applications to communicate, we need the following:

These are the main applications for using sockets:

There are two types of communication between applications:

The sockets allow us to implement a client-server or peer-to-peer architecture. The communication must be initiated by one of the programs, which is called the client program. The second program waits for another to initiate the communication. For this reason, it is called the server program.

A socket is a process or thread that exists in the client machine and in the server machine, with the objective that the server and the client read and write the information. This information will be transmitted by the different network layers.

When a client connects with the server, a new socket is created. The server can continue to wait for a connections in the main socket and communicate with the connected client, in the same way a socket is established in a specific port of the client.

A server application usually listens for a specific port that is waiting for a connection request from a client; once it is received, the client and the server are connected so that they can communicate. During this process, the client is assigned to a port number, through which they send requests to the server and receive the responses from it.

Similarly, the server obtains a new local port number that will continue listening to each connection request of the original port.

Sockets are a universal feature in any programming language, and also without limits; an application made in PHP can communicate with another made in Java and vice versa, or an application made in Python can communicate with another made in C.

Thanks to this feature, we have browsers, mail clients, and FTP clients that work and communicate with the servers, regardless of the operating system, technology, or programming language.