Detailed explanation of TCP communication process and tcp long connection and short connection

1. TCP connection

When the TCP protocol is used for network communication, a connection must be established between the server and the client before the actual read and write operations. After the read and write operations are completed, the two parties can release the connection when they no longer need the connection. A three-way handshake is required, and four handshakes are required for release, so the establishment of each connection requires resource consumption and time consumption

The classic three-way handshake diagram:

picture

The classic four-wave hand chart:

picture

Ten state diagrams of tcp:

picture

Notice:

When one end receives a FIN, the kernel makes read return 0 to notify the application layer that the other end has terminated the data transmission to the end; sending FIN is usually the result of the application layer closing the socket.

2MSL problem of tcp

picture

2. TCP short connection

Let's simulate the situation of a TCP short connection. The client initiates a connection request to the server, the server receives the request, and then the two parties establish a connection. The client sends a message to the server, the server responds to the client, and then a read and write is completed. At this time, either of the two parties can initiate a close operation, but generally the client initiates the close operation first. Why, the general server will not close the connection immediately after replying to the client. Of course, there are special circumstances. From the above description, a short connection generally only transmits a read and write operation between client/server

The advantage of short connections is that it is relatively simple to manage, and the existing connections are all useful connections without additional control means

3. TCP long connection

Next, let's simulate the situation of a long connection. The client initiates a connection to the server, the server accepts the client connection, and the two parties establish a connection. After the client and server complete a read and write, the connection between them will not be closed actively, and subsequent read and write operations will continue to use this connection.

First, let’s talk about the TCP keep-alive function mentioned in the detailed explanation of TCP/IP. The keep-alive function is mainly provided for server applications. The server application wants to know whether the client host crashes, so that it can use resources on behalf of the client. If the client has disappeared, leaving a half-open connection on the server, and the server is waiting for data from the client, the server will wait for the data from the client, and the keep-alive function is to try to detect this half-open connection on the server side. connect.

If a given connection has no action within two hours, the server sends a probe segment to the client, and the client host must be in one of the following four states:

The client host is still up and reachable from the server. The client's TCP response is normal, and the server also knows that the other party is normal, and the server resets the keep-alive timer after two hours. The client host has crashed and is shutting down or is being restarted. In either case there is no response from the client's TCP. The server will not receive a response to the probe and will time out after 75 seconds. The server sends a total of 10 such probes, each with an interval of 75 seconds. If the server does not receive a response, it assumes that the client host is closed and terminates the connection. The client host crashed and has been restarted. The server will receive a response to its keep-alive probe, which is a reset, causing the server to terminate the connection. The client is running normally, but the server is unreachable. This situation is similar to 2. What TCP can find is that it has not received a probe response. It can be seen from the above that the TCP keep-alive function is mainly to detect the survival status of the long connection, but there is a problem here, the detection period of the survival function is too long, and it is only to detect the survival of the TCP connection, which is a relatively gentle approach. When encountering a malicious connection, the keep-alive function is not enough.

In the application scenario of long connection, the client side generally will not actively close the connection between them. If the connection between the client and the server has not been closed, there will be a problem. With more and more client connections, the server Sooner or later, when you can’t handle it, the server needs to adopt some strategies at this time, such as closing some connections that have not occurred for a long time to read and write events, so as to avoid some malicious connections that cause server-side service damage; The client machine is granular, limiting the maximum number of long connections for each client, which can completely prevent a bad client from compromising the backend service.

The generation of long connection and short connection depends on the closing strategy adopted by the client and server. Specific application scenarios adopt specific strategies. There is no perfect choice, only a suitable choice.

What are "long connections" and "short connections"?

Explanation 1

The so-called long connection means that after the SOCKET connection is established, the connection is maintained regardless of whether it is used or not, but the security is poor;

The so-called short connection means that after the SOCKET connection is established, the connection is disconnected immediately after the data is sent and received. Generally, banks use short connections.

Explanation 2

A long connection means that in a tcp-based communication, the connection is always maintained, regardless of whether data is currently being sent or received.

The short connection is to connect only when there is data transmission, and the client-server communication/transmission of data is completed and the connection is closed.

Explanation 3

The concept of long connection and short connection seems to be mentioned only in the mobile CMPP protocol, and has not been seen elsewhere. Communication mode There are two connection modes between network elements: long connection and short connection. The so-called long connection means that multiple data packets can be sent continuously on a TCP connection. During the maintenance period of the TCP connection, if no data packets are sent, both parties need to send detection packets to maintain the connection. A short connection means that when the communication parties have data interaction, a TCP connection is established, and after the data transmission is completed, the TCP connection is disconnected, that is, each TCP connection only completes the sending of a pair of CMPP messages. At this stage, it is required to adopt the communication mode of long connection between ISMGs, and it is recommended to adopt the communication mode of long connection between SP and ISMG.

Explanation 4

Short connection: such as http, just connect, request, close, the process time is short, if the server does not receive a request for a period of time, it can close the connection. Long connection: Some services need to connect to the server for a long time, such as CMPP, and generally need to maintain online by themselves.

Long and short connections of HTTP protocol

1. Long connection and short connection:

Long connection: The client side and the server side establish a connection first, and the connection is not disconnected after the connection is established, and then the message is sent and received. In this way, the communication connection always exists. This method is often used in P2P communication. Short connection: The client and the server communicate with each other every time they send and receive a message, and disconnect immediately after the transaction is completed. This method is often used in point-to-multipoint communication. C/S communication. 2. The operation process of long connection and short connection:

The operation steps of short connection are: establish connection - data transmission - close connection... establish connection - data transmission - close connection The operation steps of long connection are: establish connection - data transmission ... (keep connection) ... data transmission - — close the connection 

3. When to use long and short connections:

Long connection: long connection is mostly used for frequent operations, point-to-point communication, and the number of connections should not be too many. The establishment of each TCP connection requires a three-way handshake, and the disconnection of each TCP connection requires a four-way handshake. If you have to establish a connection for each operation and then operate again, the processing speed will decrease, so after each operation, you can directly send data in the next operation, and you don’t need to establish a TCP connection. For example: the connection of the database uses a long connection. If a short connection is used for frequent communication, it will cause socket errors, and frequent socket creation is also a waste of resources.

Short connection: HTTP services of web sites generally use short connections. Because the long connection consumes a certain amount of resources for the server. Thousands or even hundreds of millions of client connections such as web sites use short connections to save some resources. Just imagine if you use long connections and thousands of users at the same time, and each user occupies a connection, you can imagine how much pressure the server will have. So the amount of concurrency is large, but each user needs a short connection when they do not need frequent operations. In short: the choice of long connection and short connection depends on the demand.

4. Sending and receiving method:

1. Asynchronous: message sending and receiving are separated, independent of each other, and do not affect each other. This method is divided into two situations: asynchronous duplex: receiving and sending are in the same program, and two different sub-processes are responsible for sending and receiving respectively. Asynchronous simplex: pick-up and send are done using two different programs. 2. Synchronization: Message sending and receiving are carried out synchronously, that is, waiting for receiving and returning messages after sending messages. The synchronization method generally needs to consider the timeout problem. Imagine that we cannot wait indefinitely after sending a message, so we need to set a waiting time. When the waiting time is exceeded, the sender no longer waits for the read return message. Direct notification timeout returns.

5. Message format:

Communication message formats are more diverse, and corresponding functions for receiving and sending messages for reading and writing messages must be designed. blocking and non-blocking

1. Non-blocking mode: The read function keeps reading. If no message is received, it will wait for a period of time and return after a timeout. In this case, a timeout period needs to be specified.

2. Blocking mode: If no message is received, the read function is in a waiting state until the message arrives.

Cyclic reading and writing

1. One-time direct reading and writing of messages: read or send all message bytes at one time without distinction in one receiving or sending message action.

2. Circular reading and writing with no specified length: this version occurs during the short connection process, limited by network routing, etc., a long message may be decomposed into many packets during network transmission, and it may not be possible to read all of them at one time. After reading the message once, it is necessary to read the message in a loop until it is finished.

3. Circular reading and writing of message headers with length: This situation is generally in a long connection, because there is no condition in a long connection to judge when the cyclic reading and writing ends. The length header must be added. The read function first reads the length of the message header, and then reads the message according to this length. In actual situations, the format of the header code system is often different. If it is a non-ASCII message header, it must be converted into ASCII

Common message headers are compiled as follows: 1. ASCII code of n bytes. 2. BCD code of n bytes. 3. A network integer code of n bytes. The above are several typical ways of reading and writing messages, and some typical API reading and writing functions can be provided in advance together with the communication mode template. Of course, in practical problems, it may be necessary to write a reading and writing API that matches the message format of the other party. In actual situations, it is often necessary to connect our own system with other people's systems. With the above templates and APIs, it can be said that the connection There is no problem with the communication procedure in any way.

When to use long connection, short connection?

Long connections are mostly used for frequent operations and point-to-point communications, and the number of connections should not be too many. Each TCP connection requires a three-step handshake, which takes time. If each operation is connected first, then the processing speed will be reduced a lot, so each operation will not be disconnected, and the data packet will be sent directly during the first processing. It's OK, no need to establish a TCP connection. For example: the database connection uses a long connection, if a short connection is used for frequent communication, it will cause socket errors, and frequent socket creation is also a waste of resources.

HTTP services like WEB websites generally use short links, because long connections will consume a certain amount of resources for the server, and it will be more economical to use short connections for tens of thousands or even hundreds of millions of client connections like WEB websites. For some resources, if you use long connections and there are thousands of users at the same time, if each user occupies a connection, you can imagine it. Therefore, the amount of concurrency is large, but it is better to use short connections when each user does not need to operate frequently. In short, the choice of long connection and short connection depends on the situation.

Guess you like

Origin blog.csdn.net/LinkSLA/article/details/131846500