Long connection and short connection analysis

Source: Internet
Author: User
Tags socket error

transferred from: http://www.cnblogs.com/heyonggang/p/3660600.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.

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

Explanation 1

The so-called long connection refers to the establishment of a socket connection, regardless of whether or not to remain connected, but poor security;

The so-called short connection refers to the establishment of a socket connection after the data received after the end of the connection, the general bank used short connection

Explanation 2

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

A short connection is a connection that is only made when there is data transfer, and the client-server communication/Transfer data is closed when the connection is completed.

Explanation 3

The concept of long connections and short connections seems to be only mentioned in the moving CMPP protocol, where other places have not been seen.
Communication mode
There are two types of connections between the various network elements: long connections and short connections. The so-called long connection, refers to a TCP connection can be continuously sent multiple packets, during the TCP connection remains, if no packet is sent, both sides need to send a detection packet to maintain this connection. Short connection refers to the communication between the two sides of the data interaction, a TCP connection is established, after the data is sent, the TCP connection is disconnected, that is, each TCP connection completes only a pair of CMPP messages sent.
At present, it is necessary to use long-connected communication mode between ISMG, and it is recommended to use long connection between SP and ISMG.

Explanation 4

Short connection: For example, HTTP, just connect, request, close, the process time is short, the server will not receive a request for a period of time to close the connection.
Long connections: Some services need to be connected to the server for a long time, such as CMPP, which usually needs to be maintained online.

Long, short connection of HTTP protocol

One, long connection and short connection:
Long connection: The client and the server to establish a connection, the connection after the establishment of continuous open, and then send and receive messages.
This means that the communication connection is always present. This approach is commonly used for peer-to-peer communication.
Short connection: A communication connection is made between client and server each time a message is sent and received, and disconnected immediately after the transaction is completed.
This approach is often used in a point-to-multipoint communication. c/S communication.
Two, long connection and short connection operation process:
The operation steps for a short connection are:
Establish connection--data transfer--close connection ... Establish connection--data transfer--close connection
The procedure for long connections is:
Establish connection--data transfer ... (Keep connected) ... Data transfer--close connection
Three, long connection and the use of short connection time:
Long connections: Long connections are used for frequent, point-to-point communication, and the number of connections can not be too many cases.
Each TCP connection is established with a three-time handshake, and each TCP connection disconnects four times.
If each operation is to establish a connection and then the operation of the processing speed will be reduced, so after each operation, the next time the operation of the direct data can be sent, no longer establish a TCP connection. For example: Database connection with a long connection, if a short connection with frequent communication will cause a socket error, frequent socket creation is also a waste of resources.
Short connections: HTTP services for Web sites are generally short-connected. Because a long connection consumes a certain amount of resources for the server. Thousands or even billions of clients, such as Web sites, connect more resources with short connections. Imagine if all with a long connection, and at the same time with thousands of users, each user has a connection, it can be imagined how much pressure on the server. Therefore, the concurrency is large, but each user does not need to operate frequently in the case of short connections. In short: the choice of long connections and short connections depends on the requirements.
Iv. send and Receive mode:
1, asynchronous: Message sending and receiving are separate, independent of each other, non-impact. This approach is divided into two different situations:
Asynchronous duplex: Receive and send in the same program, there are two different sub-processes responsible for sending and transporting.
Asynchronous simplex: Transfer and send using two different programs to complete.
2, synchronization: Message sending and receiving is synchronous, that is, after the message sent to wait for the return of the message. Synchronous mode generally need to consider the time-out problem, think we send a delivery paper can not wait indefinitely ah, so we have to set a wait
When The sender of the wait time is no longer waiting for the read return message. Direct notification timeout returned.
V. Format of the message:
Communication message format is more diverse, corresponding to the need to design the corresponding read and write message receiving and sending a paper function.
Blocking and non-blocking modes
1, non-blocking mode: Read function continuously read action, if no message received, wait for a period of time after the return, this situation generally need to specify the time-out.
2, blocking mode: If the message is not received, the Read function has been in a waiting state, know that the message arrives.
Cyclic read/write mode
1, a direct read and write message: In a single receive or send a newspaper text action one time without adding the full read or all the newspaper text section.
2, do not specify the length of the cycle read and write: This version occurs in the short connection process, by network routing and other restrictions, a longer message may be broken down into a number of packets in the network transmission process, a read may not be all read a message, which requires a loop to read the message until the end of reading.
3, with the length of the message head cycle Read and write: This situation is generally in a long connection, because there is no condition in the long connection to determine when the loop read and write end. Length headers must be added. Read function First read the length of the message header, and then according to the length of the newspaper text, the actual situation, the header code format is often different, if the non-ASCII message header, but also must be converted to ASCII common message header programming:
1, n bytes of ASCII code.
2, n bytes of BCD code.
3, n bytes of network integer code.

These are some of the more typical read-write messages, and can provide some typical API reading and writing functions in conjunction with the communication mode template. Of course, in practical problems, you may also have to write a read-write API that is compatible with the other message format. In the actual situation, often need to put our own system and other people's system to connect, with the above template and API, can be said to connect any way of communication programs are not problematic.

Source: http://www.360doc.com/content/14/0412/16/16726605_368309628.shtml

When to use long connections, short connections?
Long connections are used to operate frequently, point-to-point communications, and the number of connections can not be too many cases. Each TCP connection requires a three-step handshake, which takes time, if each operation is first connected, then operation then the processing speed will be reduced a lot, so after each operation is continuously open, the processing of the direct sending of packets is OK, do not establish a TCP connection. For example, the connection of the database with a long connection, if the frequent communication with short connections will cause socket errors, and frequent socket creation is also a waste of resources. HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server, and tens of thousands or even billions of clients, such as Web sites, use short connections to save resources if they are connected with long connections, and thousands of users at the same time, If each user occupies a connection, then you can imagine it. So the concurrency is large, but each user needs to be short-connected in case of frequent operation.

In summary, the choice of long connections and short connections is subject to availability.

The server side of the company uses resin to do middleware, interacting with clients sending requests every few seconds.
This is supposed to be a short connection, right?
Short connections require frequent setup and disconnection, is it a waste of resources on the server?
What if we switch to a growth connection? Where are the disadvantages of long connections?

Reply:

From the network technology level: TCP itself is long-connected.
Of course, from the business level: each connection only processing a single request can be called a short connection, after processing the business to continue to open the connection but wait for the next pen can be called a long connection.
As for the actual situation need to use short or long connection, mainly look at the real-time requirements, data flow and concurrency of the three problems.
Since you did not specify the characteristics of the request on these three questions, I cannot give you any specific advice.
The advantages of long connection: Save TCP handshake time, can guarantee high real-time, data flow can adopt server-side active push mode.
Long connection disadvantage: concurrency should not be too high, continue to occupy the service port (relative consumption of resources).


I have a simple sample of a chat room based on a long-link push model, and you can look at:
http://blog.csdn.net/ldh911/article/details/7268879

1. There is no real-time chat between the player and the player in the game, and the system can not notify the player in time when there is mail or information.
-If it comes to chatting, it is generally more appropriate to use a long connection, otherwise a lot of time is wasted on the handshake;
--but the network quality of the phone's long-connected network may be a pinch, and you need to seriously consider fault tolerance and heavy-chain mechanisms.

2. The client sends a request every few seconds, so the pressure on the server is not great?
-The pressure will be relatively large, the key is the chat is often a high demand for time, if it is a group of war, 1 seconds did not see the information, it may feel completely unbearable, of course, also see how you chat scenes, is a group chat or a single chat, will not develop into voice what;

NIO does not have any problems, the mainstream of large-scale long-connection processing is NIO, and it is not invented by Java, itself is the use of the operating system network management capabilities.

Long connection and short connection analysis

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.