(reprint) The difference between a long connection and a short connection (TCP socket HTTP concept principle)

Source: Internet
Author: User
Tags socket error

Original address: http://blog.sina.com.cn/s/blog_6d39b5be0101k6v4.html

    

A 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.

Two, long connection and short connection operation procedure: the short connection operation steps are: establishes the connection--data transmission--Closes the connection ... Establish connection--data transfer--close connection
The operation of the long connection is: Establish a connection-data transmission ... (Keep connected) ... Data transfer--close connection

Three, long connection and short connection of the use of time: Long connection: A short connection for frequent operation, 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 to establish a connection and then the operation of the processing speed will be reduced, so each time the operation of the next operation to send data directly, 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 summary: 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. There are two cases in this way: Asynchronous Duplex: Receive and send in the same program, there are two different sub-processes responsible for sending and transfer. 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 paper after the text can not be infinite wait ah, so we have to set a waiting time. The sender of the wait time is no longer waiting for the read return message. Direct notification timeout returned.

V. Message format: The diversity of Communication message format is more, accordingly must design corresponding read and write message receive and send the newspaper 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 until the message arrives.
Circular reading and writing 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, this needs to loop read the message, know that 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.

(reprint) The difference between a long connection and a short connection (TCP socket HTTP concept principle)

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.