Popular explanation of what is Socket communication

Socket communication principle

1. What is Socket?

Socket , the socket. It is the endpoint of the logical connection between two hosts . (In layman's terms: two programs on the network exchange data through a two-way communication connection, and one end of this connection is called a socket).

Socket is a set of APIs for communication between different hosts. It works on top of our TCP/IP protocol stack and can be applied to browsers, mobile applications, or SSH clients for server management.

To establish communication with different hosts through Socket, we only need to specify the IP address of the host and a port number.

  • The IP address is used to uniquely identify your network device

  • Ports are mainly used to distinguish different applications on the host (without ports, the operating system has no way to distinguish which application the data should be sent to)

Through Socket, we can establish a virtual data channel between different hosts and different applications, and it is point-to-point (application-to-application), which can be compared to connecting a data line to the socket of different applications. It is also the origin of the name socket.

2. Socket type

There are two types of commonly used Socket: TCP and UDP

2.1 TCP - Transmission Control Protocol

Feature 1: TCP is reliable, and its underlying layer will automatically detect and return lost data packets, so for the caller, the data you send will definitely be received by the other party.

Feature 2: The order of data sent and received is exactly the same. For example, if you send a character, the other party will definitely receive the same character string intact. This is why TCP is based on "data stream" protocol.

TCP requires both parties to send and receive data to play different roles: server and client.

The server will passively wait for the connection from the client, and it will not actively initiate the request itself.

2.2 UDP-User Message Protocol

UDP sends and receives data in units of datagrams, and UDP does not automatically return lost data packets, so there is no guarantee that the data will be received by the other party.

Because of the lack of these checks, UDP usually has lower latency and takes up less system resources, and it is more suitable for applications with high real-time requirements such as video and voice calls.

3. Transfer data between two ends

Through Socket, we can establish a connection with a certain machine, if we want to transfer data between the two ends:

Execute the socket() method at both ends. After obtaining the fd handle, execute the bind(), listen(), and accept() methods for the server in turn, and then wait for the connection request from the client to execute the connect() method to initiate a connection to the server. After the connection is established, the client can execute the send() method to send the message, and the server can execute the recv() to receive the message.

Conversely, the server can also execute send() to send messages, and the client can execute recv() to receive messages.

How does the server distinguish between multiple clients?

The data packet sent by the client will have the source IP address and port, as well as the destination IP address and port. These four elements form a quadruple, which can be used to uniquely mark a client.

Guess you like

Origin blog.csdn.net/beiye_/article/details/130228486