Persistent connection and short connection

Source: Internet
Author: User
Persistent connection and short connection

A persistent connection means that multiple data packets can be sent continuously over a TCP connection. If no data packet is sent during TCP connection persistence, both parties need to send a detection packet to maintain this connection, generally, you need to maintain it online.
A short connection is a TCP connection established when both parties have data interaction. After the data is sent, the TCP connection is disconnected. Generally, banks use short connections.
For example, HTTP only supports connection, request, and close. If the server does not receive a request within a certain period of time, the connection is closed.
In fact, persistent connections are relative to common short connections, that is, to maintain the connection between the client and the server for a long time.

Procedure

 

The common short connection procedure is as follows:
Connection → data transmission → close connection;


The persistent connection is usually:
Connection → data transmission → keep connection (Heartbeat) → ...... → Close the connection;
This requires that the persistent connection send packets (Heartbeat) regularly when there is no data communication to maintain the connection status. The short connection can be closed directly when there is no data transmission.

When can I use persistent connections or short connections?

Persistent connections are mostly used for frequent operations and point-to-point communication, and the number of connections cannot be too large ,. Each TCP connection requires three-step handshake, which takes time. If each operation is connected first and then operated, the processing speed will be much lower, so each operation will continue to run after completion, it is OK to directly send the data packet during the next processing, so no TCP connection is required. For example, if you use persistent connections for database connections, frequent communication may cause socket errors, and frequent socket creation is a waste of resources.

 

HTTP Services for websites generally use short links, because long connections consume resources for the server, however, the frequent connection of thousands or even hundreds of millions of clients to web sites with short connections saves some resources. If persistent connections are used and there are thousands of users at the same time, if every user occupies a connection, you can imagine it. Therefore, a large amount of concurrency is required, but each user needs to be connected in a short connection without frequent operations.

 

In short, the choice of persistent connection and short connection depends on the situation.

 

 

 

 

Sending and receiving Method

1. asynchronous
Messages are sent and received separately and independent from each other. This method can be divided into two situations:
(1) asynchronous duplex: In the same program, two different sub-processes are responsible for sending and receiving respectively.
(2) asynchronous Ticket: two different programs are used for receiving and sending.
2. Synchronization
Message sending and receiving are both synchronous, waiting for receiving the returned message after the message is sent. The timeout issue needs to be considered in the synchronization mode, that is, the sender cannot wait infinitely after the message is sent out, and the timeout time must be set. If the timeout time is exceeded, the sender no longer waits for the read return message, directly notifying the timeout response.

 

In a persistent connection, there is generally no condition to determine when the read/write ends. Therefore, a length packet header must be added. The READ function first reads the length of the packet header, and then reads the packets of the corresponding length according to the length.

 

 

Ticket, half duplex, and full duplex

Based on the division of labor between the two parties and the signal transmission direction, the communication can be divided into three modes: Single-work, half-duplex and full-duplex. In computer networks, the dual mode is used. The LAN adopts the half-duplex mode, and the man and Wan adopt the full dual-year mode. 1. Ticket (simplex): the sender and receiver in the Communication Equipment of both parties have a clear division of labor and can only transmit data in a single fixed direction from the sender to the receiver. Typical sending devices such as card readers of early computers and typical receiving devices such as printers communicate with each other. 2. Half Duplex (Half Duplex) mode: both devices on both sides of the communication are both transmitters and receivers. The two devices can transmit data to each other, but data can only be transmitted to one direction at a time. For example, a walkie talkie is a half-duplex device, because only one party can speak at a time. 3. Full Duplex Mode: the communication device is both a transmitter and a receiver, and the two devices can transmit data in both directions at the same time. For example, the phone is a full-duplex device because both parties can talk at the same time.

 

From: http://jiangzhengjun.iteye.com/blog/502966

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.