The difference between a TCP long connection and a short connection

Source: Internet
Author: User

Original address: http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html

1. TCP Connection

When the TCP protocol is used in network communication, before the actual read and write operation, the server and the client must establish a connection, when the read and write operation is completed, the two sides no longer need the connection when they can release the connection, the connection is established to need three handshake, and release will require 4 handshake, So the establishment of each connection requires resource consumption and time consumption.

Classic three-time handshake:

Classic four-time handshake close diagram:

2. TCP Short Connection

We simulate the case of a TCP short connection, the client initiates a connection request to the server, the server receives the request, and the two sides establish the connection. The client sends a message to the server, the server responds to the client, and then a read-write is completed, and either of the parties can initiate a close operation, but usually the client initiates the close operation. Why, the general server will not reply to the client immediately after closing the connection, of course, do not rule out a special situation. From the above description, short connections generally only pass a read and write operation between Client/server

The advantage of short connections is that they are simpler to manage, that existing connections are useful connections, and that no additional control is required

3.TCP Long Connections

Next we simulate the case of a long connection, the client initiates a connection to the server, the server accepts the client connection, and the two sides establish the connection. After the client and server have completed a read and write, the connection between them is not actively closed, and subsequent read and write operations continue to use the connection.

First of all, the TCP/IP feature described in the TCP/IP details, the KeepAlive function is mainly provided for the server application, the server application wants to know whether the customer host crashes, which can be used on behalf of the customer resources. If the customer has disappeared, leaving a semi-open connection on the server while the server is waiting for data from the client, the server should be waiting for the client's data, and the KeepAlive function is trying to detect this semi-open connection on the server side.

If a given connection does not have any action within two hours, the server sends a probe segment to the client and the client host must be in one of the following 4 states:

    1. The client host is still functioning and can be reached from the server. The client's TCP response is normal, and the server knows the other side is normal, the server after two hours will be keepalive timer reset.
    2. The client host has crashed and is shutting down or restarting. In either case, the client's TCP is not responding. The server will not receive a response to the probe and time out after 75 seconds. The server sends a total of 10 such probes at each interval of 75 seconds. If the server does not receive a response, it considers the client host to be closed and terminates the connection.
    3. The client host crashed and has restarted. The server receives a response to its keepalive probe, which is a reset that causes the server to terminate the connection.
    4. The client runs normally, but the server is unreachable, which is similar to 2, where TCP can find no response to the probe.

As can be seen from the above, the TCP keepalive function is mainly to detect the survival of long connections, but there is a problem, the survival function of the detection period is too long, there is it is only to detect the survival of TCP connections, is a relatively gentle practice, encounter malicious connection, the keepalive function is not enough.

In the case of long-connected applications, the client side generally does not actively shut down their connection between the client and the server if the connection is not closed, there will be a problem, as the client connection more and more, the server sooner or later can not carry the time, At this time, the server side needs to take some policies, such as the closure of some long-term no read and write events occurred connection, so as to avoid some malicious connection caused server-side service damage, if the condition is allowed to the client machine for granularity, limit the maximum number of connections per client, This can completely avoid an egg-hurting client from compromising the backend service.

The generation of long connections and short connections is based on client and server shutdown policies, specific application scenarios with specific strategies, no perfect choice, only the right choice.

Reference:

1. TCP/IP Detailed Volume One

Transferred from: http://www.cnblogs.com/beifei/archive/2011/06/26/2090611.html

The difference between a TCP long connection and a short connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.