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:

  • Server (the listener): The server always listens for communications in a specific port.
  • Client: Normally, the client connects to the server through the port and starts sending requests and waiting for answers.
  • Transmission channel: This can be a port of entry for the server and an exit port for the client.
  • Protocol: This is the topic of conversation. For two applications to communicate, they must be programmed to answer each other.

These are the main applications for using sockets:

  • Server: Application that is waiting for the client to connect
  • Client: Application that connects to the server
  • Client/server: Application that is a client and server at the same time, for example, a chat application, that can send messages to other applications and, at the same time, wait for other applications to send messages to it

There are two types of communication between applications:

  • Local: When the applications are on the same computer, the 127.0.0.1 IP address or localhost is used
  • Remote: When the applications are on different computers, the client application connects the IP address and server port

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset