Socket long connection with short connection, heartbeat

Source: Internet
Author: User
long connection and short connection

The so-called long connection, refers to a TCP connection can send multiple packets in a row, in the TCP connection to maintain, if there is no packet sent, the need for both sides to send detection packets to maintain this connection, the general need to do their own online maintenance.
Short connection refers to the communication between the two parties have data interaction, the establishment of a TCP connection, after the data is sent, then disconnect this TCP connection, the general bank uses a short connection.
For example, HTTP, just connect, request, shutdown, process time is short, the server if not received a request for a period of time to close the connection.
In fact, long connection is relative to the usual short connection, that is, long time to maintain the client and server connection state. the operation process of long connection and short connection

The usual short connection procedure is:
Connection → data transfer → close connection;


And long connections are usually:
Connection → data transmission → stay connected (heartbeat) → data transmission → keep connected (heartbeat) → ... → Close connection;
This requires long connection in the absence of data communication, timed to send packets (heartbeat) to maintain the connection status, short connection in the absence of data transmission directly close on the line when the long connection, short connection.

Long connection is often used for frequent operation, point-to-point communication, and the number of connections can not be too much. Each TCP connection requires a three-step handshake, which takes time, if each operation is the first connection, then the processing speed will be reduced a lot, so after each operation is constantly open, secondary processing directly send packets on OK, do not establish a TCP connection. For example: A database connection with a long connection, if the use of short connection frequent communication will cause socket errors, and frequent socket creation is also a waste of resources.

Send receive Mode

1, asynchronous
Message sending and receiving are separate, independent, and do not affect each other. This approach is divided into two different situations:
(1) Asynchronous duplex: Receive and send in the same program, two different sub processes are responsible for sending and receiving
(2) Asynchronous Simplex: receiving and sending is done with two different programs.
2, synchronization
Message sending and receiving is synchronous, both message sending and waiting for receiving return message. The synchronization method generally needs to consider the timeout problem, that is, the message sent out after not infinite wait, need to set the timeout time, more than the time the sender is no longer waiting to read the return message, direct notification timeout return.

In the long connection there is generally no condition to determine when reading and writing to end, so must be added to the length of the message header. The reading function reads the length of the packet head, and then reads the corresponding length of the message according to the length.

single, half duplex and full duplex

According to the Division of communication between the two parties and signal transmission direction can be divided into three kinds of communication methods: Simplex, Half-duplex and Full-duplex.   In the computer network mainly uses the duplex way, among them: The local area network uses the half duplex way, the metropolitan area net and the WAN uses the whole double year way. 1. Simplex (Simplex): Communication between the two devices in the transmitter and receiver division of labor, can only be sent to the receiver by the transmitter in a single fixed direction of data transmission.   A typical sending device, such as an early computer card reader, that uses a single-work communication, typically a receiving device such as a printer. 2. Half-duplex (Half Duplex) mode: Communication devices are both transmitter and receiver, two devices can transmit data to each other, but at a certain moment can only send data in one direction.   For example, a walkie-talkie is a half-duplex device because only one side can speak at a time. 3. Full-duplex (full Duplex): Communication devices are both transmitter and receiver, two devices can transmit data in two directions at the same time. For example, the phone is a full-duplex device because both sides can talk at the same time.


HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server, the connections of thousands or even billions of clients, such as Web sites, are more resource-intensive, if connected with long, and thousands of users, Imagine if each user uses a connection. Therefore, a large number of concurrent, but each user without frequent operation of the need for a short link good.


In short, the choice between long and short connections depends on the situation.

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.