HTTP persistent connection and short connection

Source: Internet
Author: User
Tags socket error
From: Http://blog.csdn.net/camel_flying/article/details/6056910 1. persistent connection and short connection:
 
Persistent connection: The client establishes a connection with the server. After the connection is established, the connection continues to open and then sends and receives packets.
 
In this mode, communication connections always exist.This method is usually used for P2P communication.
 
Transient connection: The client and server communicate with each other only when sending and receiving packets. The connection is closed immediately after the transaction is completed.
 
This method is usually used for one-to-multiple-point communication. C/s communication.
Ii. Operation Process of persistent connection and short connection:
 
The procedure of transient connection is as follows:
Establish connection -- data transmission -- close connection... establish connection -- data transmission -- close connection
 
The procedure for persistent connection is:
 
Establish connection -- data transmission... (keep connection)... data transmission -- close connection
Iii. Time to use persistent connection and short connection:
 
Persistent connectionShort connections are mostly used for frequent operations, point-to-point communication, and the number of connections cannot be too large.
 
Three handshakes are required for the establishment of each TCP connection, and four handshakes are required for the disconnection of each TCP connection.
 
If a connection needs to be established for each operation, the processing speed will be reduced.
 
You can directly send data during the next operation without establishing a TCP connection. For example, persistent connections are used for database connections,
If a short connection is used for frequent communication, a socket error may occur. Frequent socket creation is a waste of resources.
 
Transient connection: HTTP Services for web sites generally use short connections. Because persistent connections consume certain resources for servers.
 
Connections to thousands or even hundreds of millions of clients such as web sites frequently use short connections to save some resources. If persistent connections are used,
 
In addition, if thousands of users and each user occupies a connection, we can imagine the pressure on the server.
 
Therefore, a large number of concurrent connections are required, but each user does not need to perform frequent operations.
 
In short, the choice of persistent connection and short connection depends on the needs.
4. Sending and receiving methods:
1. asynchronous:Messages are sent and received separately and independent from each other. This method can be divided into two situations:
 
Asynchronous duplex: receives and sends messages in the sameProgramThere are two different sub-processes responsible for sending and transferring.
 
Asynchronous Ticket: two different programs are used for sending and picking.
 
2. Synchronization: The packet is sent and received synchronously, that is, the packet is waiting for the return packet after the packet is sent. Synchronization mode
 
We generally need to consider the timeout issue. Imagine that we cannot wait infinitely after sending the message, so we need to set a wait
 
.Beyond the waiting time, the sender no longer waits for the read Response Message. Direct notification timeout is returned.
5. Message format:
Communication message formats are more diverse, and the corresponding read/write packets must be designed accordingly.
Function for receiving and sending packets.
Blocking and non-blocking Modes
 
1. Non-Blocking Mode: The READ function does not stop reading. If no message is received, wait for a period of time before returning,
 
In this case, you need to specify the timeout value.
 
2. Blocking Mode: If no message is received, the READ function remains in the waiting state and knows that the message has arrived.
Cyclic read/write mode
 
1. One direct read/write Packet: All or all sent bytes are read at one time without adding.
2. Circular read/write with no Length Specified: This version occurs in the short connection process. Due to network routing and other restrictions, a long packet may
 
In the network transmission process, it is divided into many packages. One read may not be able to read all the packets once, which requires reading packets cyclically,
 
So far.
 
3. Loop read/write with length packet header: This situation is generally in a persistent connection, because there is no condition in the persistent connection to determine when the cyclic read/write ends.
 
The Length header must be added. The READ function first reads the length of the packet header and then reads the packet based on the length. In actual situations, the Header Format is often different,
 
If it is a non-ASCII packet header, it must also be converted into common ASCII packet headers:
 
1. ASCII code of n Bytes.
2. n Bytes of BCD code.
 
3. A network integer of n Bytes.
 

The above are several typical read/write messages. You can provide some typical API read/write functions in advance with the communication mode template..

Of course, in practice, you may have to write a read/write API that matches the recipient's message format.In actual situations

Connect our own system with others' systems. With the above templates and APIs, we can say that we can connect communication programs in any way.

There is no problem.

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.