The difference between a TCP long connection and a short connection

Source: Internet
Author: User
Tags keep alive

Turn from: http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html

1. TCP Connections

When the TCP protocol is used in network communication, before a true read-write operation, a connection must be established between the server and the client, and when the read and write operation is complete, they can release the connection when they no longer need the connection, and the connection is established with a handshake of three times, and the release takes 4 shakes. So each connection is built to require resource consumption and time consumption.

Classic three-time handshake schematic:

Classic four-time handshake close diagram:

2. TCP Short Connection

We simulate the 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 and write is done, and either of the two sides can initiate a close operation, although it is generally the client that initiates the next action. Why, the general server will not reply to the client immediately after the shutdown of the connection, of course, do not exclude special circumstances. From the above description, short connections typically only pass a read-write operation between Client/server

The advantage of a short connection is that it is simpler to manage, the connections that exist are useful connections, and no additional control is required.

3.TCP Long Connection

Next we simulate the long connection, the client initiates a connection to the server, the server accepts the client connection, and the two sides establish the connection. Once 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 explained in detail on the TCP live function, the main function for the server application, server applications want to know whether the customer host crashes, so that customers can use resources on behalf of. If the customer has disappeared, leaving a half-open connection on the server, and the server is waiting for data from the client, the server should be far from waiting for the client's data, which is trying to detect the Half-open connection on the server side.

If a given connection does not have any action within two hours, the server sends a probe message segment to the client, and the client host must be in one of the following 4 states: The client host is still running and can be reached from the server. The client's TCP response is normal, and the server is aware that the other is normal, the server in two hours after the live timer reset. 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 be able to receive a response to the probe and timeout after 75 seconds. A total of 10 such probes are sent by the server, each interval of 75 seconds. If the server does not receive a response, it believes that the client host has shut down and terminated the connection. The client host crashed and has restarted. The server will receive a response to its live probe, which is a reset that causes the server to terminate the connection. The client is running normally, but the server is unreachable, and this is similar to 2, where TCP can find no response from the probe.

As you can see from the above, the main function of TCP protection is to detect the survival of long connection, but there is a problem, the survival function of the detection cycle is too long, there is it is only to detect the survival of TCP connections, belong to a more refined approach, encounter malicious connection, the function is not enough to keep alive.

In a long connected application scenario, the client side generally does not actively shut down their connection, if the connection between client and server, if not shut down, there will be a problem, as the client connection is more and more, the server sooner or later can not carry the time, At this point, the server side needs to take some policies, such as closing some long time no read and write events occur, so that some malicious connections can prevent the server side of the service damage, if the conditions can be allowed to the client machine for granularity, limit the maximum number of long connections per client, This can completely avoid the pain of some of the client's backend service.

Long connection and short connection is the result of the client and server to take the shutdown strategy, specific application scenarios using specific strategies, there is no perfect choice, only the right choice.

Reference:

1. TCP/IP Detailed volume a

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

Source: http://www.cnblogs.com/liuyong/

作者喜欢研究 Sql Server ,ASP.NET MVC , Jquery WCF 等技术,同时关心分布式架构的设计应用。转载请保留原文链接,谢谢。

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.