How does a socket work?

Tram Ho

When you want the application to work realtime, Socket is a keyword that we cannot ignore.

In this article we will learn about Socket through the questions

  1. What is a socket?
  2. Why use Socket?
  3. How does the socket work?
  4. Socket classification.

What is a socket?

Socket là giao diện lập trình ứng dụng mạng được dùng để truyền và nhận dữ liệu trên internet. Giữa hai chương trình chạy trên mạng cần có một liên kết giao tiếp hai chiều, hay còn gọi là two-way communication để kết nối 2 process trò chuyện với nhau. Điểm cuối (endpoint) của liên kết này được gọi là socket.

Socket = IP adress + portnumber

Why use Socket?

Before you read this article, you also know that the main function of Socket is to “maintain connection” between the client and the server so that the application can run realtime. Such as a messaging application, Socket will be used to “listen” whenever a new message, reaction … ect to update to the interface. Updating such real-time data provides a great user experience,

If not using socket, we still have another way to update data that is continuously sending / receiving requests / responses in a certain period of time, for example, every 2 seconds we let the application check api to see If the data is changed, if any, then update it in the application. However, this process is quite “ngáo” because it causes fatigue for both the client and the server.

Socket solves this problem perfectly, saving resources for both the client and the server. So the question is how can the Socket “listen” or “stay connected” to the server to be able to update realtime data like that?

How socket works

Socket works on both TCP and UDP protocols. As mentioned above, you can see Socket = Địa chỉ IP + Số Port , the port here is the logical port of the computer (16 bits = 65535 ports).

Socket works on the 4th floor of OSI model (Transport layer)

The process of initializing the socket connection from the client to the server.

As we all know, there are ports specified by the server service for the specified services (Assigned Numbers Authority). (Port 80 is for HTTP protocol, 20 is for FTP, 22 is for SSH, 25 is for SMTP, … ect).

Initializing the connection to Gmail will take place as follows:

  1. The client with IP1 address has an idle port 5000 and decides to use the pair (IP, Port) = (IP1, 5000) to connect to the web server with IP2 and port 80 (to run the HTTP protocol => get about the website interface)
  2. After the client is successfully authenticated by the server and has all the necessary information, it will open port 25 for IP2 address. Note that the client did not send the request, but only opened port 25 for the web server.
  3. When there is a new email, the server will check if the connection to (IP1, 25) is alive or not, if it is, it will send a notification to the client.
Now the big question remains, how can the client and server maintain the connection?

At this point I can understand why the pair (IP, port) is called a socket because it goes in pairs like the two ends of a socket.

The reason the two machines can maintain the connection is because the port has been opened and will not close until the other side sends a signal to terminate by sending RST packet. In case the other party disconnects without sending RST, the connection will still be closed after a certain timeout period specified in the Keep-Alive process.

The Keep-Alive process has three properties to decide whether to close the connection or not:

  1. tcp_keepalive_time: The period of time without signal. The default is 7200s.
  2. tcp_keepalive_intvl: The amount of time that the other party responds. The default is 75s.
  3. tcp_keppalive_probles: Number of times to retry if communication failed. The default is 9.

The Keep-Alive process will take place as follows:

  1. Client opens TCP connection.
  2. After a period of tcp_keepalive_time, if the server is silent, there is no signal. The client sends the ACK flag (very small size, may not be considered a package) to the server and waits for a response.
  3. Does the server respond to the ACK?
  • (3.1) If no response: Check that the number of retries has exceeded tcp_keppalive_probles? If not, proceed to wait after the tcp_keepalive_intvl interval and resend the ACK and return to step 3. If tcp_keepalive_probes is exceeded, send RST to the server (regardless of the result returned) and close the connection.
  • (3.2) If the server responds:
    1. If the answer is correct => reset the properties and then return to step 2.
    2. If the answer is wrong => go to 3.1

Thus, by default, the client socket will open: tcp_keepalive_time + tcp_keepalive_intvl * tcp_keepalive_probes = 7200 + 75 * 9 (seconds) = 2h11p if the server’s RST is not received. However, those properties are only by default, we can completely customize them according to our needs. For example, Amazon allows customizing AWS.

Socket classification

  • Stream Socket: Based on the TCP (Tranmission Control Protocol), data transmission is only performed between two processes that have established a connection. Therefore, this form is referred to as connection-oriented socket.
    • Advantages: Can be used to communicate according to client and server models. If the client / server model, the server listens and accepts from the client. This protocol ensures data is transmitted to the recipient reliably, in the right order thanks to the network traffic management mechanism and anti-congestion mechanism. At the same time, each sent message must have a confirmation of the return and sequential packets.
    • Limitations: There is a connection (IP address) between the two processes so one of the other two processes has to wait for the other process to request a connection.
  • Datagram Socket: Based on the UDP (User Datagram Protocol) data transmission does not require a connection between the two processes. Therefore, this form is called a connectionless socket.
    • Pros: Because there is no need to establish a connection, there are no complicated mechanisms, so the protocol speed is quite fast, convenient for fast data transfer applications such as chat, game … ..
    • Limitations: In contrast to the TCP protocol, data transmitted under UDP protocol is not trusted, can be out of order and repeated.

Above is my personal understanding and understanding of sockets, hoping to bring a new perspective for you about Socket.

Share the news now

Source : Viblo