Socket stick package

Source: Internet
Author: User

Http://www.csdnjava.com/thread-27051-1-1.html

In the past two days, I have read some questions about socket stick packages and Socket buffer settings in csdn. I found that I am not very clear about them. So I will check the information and record it:

?

One or two simple concepts of persistent connections and short connections:

1. persistent connection

??? The client establishes a communication connection with the server. After the connection is established, the connection is continuously enabled, and then the packets are sent and received.

2. transient connection

??? The client and server communicate with each other every time a message is sent and received. After the transaction is completed, the connection is closed immediately. This method is usually used for one-to-multiple-point operations.

Communication, such as connecting multiple clients to a server.

?

2. When do I need to consider sticking a pack?

1: If TCP is used to send data each time, a connection is established with the other party. After both parties send a piece of data, the connection is closed, in this way, no packet sticking problem occurs (because there is only one packet structure, similar to the HTTP protocol ). Both parties must send a close connection to the closed connection (refer to the TCP closed protocol ). For example, if a needs to send a string to B, A establishes a connection with B, and then sends the default Protocol characters of both parties, such as "hello Give me something abour yourself ", after receiving the packet, B receives the data in the buffer zone and closes the connection. Therefore, the stick packet does not need to be considered, because we all know that it is sending a character.

2: If the sent data has no structure, such as file transmission, the sender only sends the data, and the receiver only receives the storage, so it does not need to consider sticking packets.

3: if the two parties establish a connection, they need to send different structured data within a period of time after the connection. For example, after the connection, there are several structures:

(1) "hello Give me something abour yourself"

2) "Don't give me something abour yourself"

?? In this case, if the sender sends the two packets consecutively, the receiver may receive the packet "hello Give me something abour yourselfdon't give me something abour yourself" at a time, so that the receiver is stupid, what is it? I don't know, because the Protocol does not stipulate such a strange string, it is necessary for both parties to organize a better package structure for packet subcontracting, therefore, a packet such as the data length may be added to the header to ensure receipt.

Cause of Three-stick packets: UDP does not stick packets during stream transmission because it has message boundaries (refer to Windows Network Programming)

1 The sender needs to wait until the buffer zone is full before sending the package, resulting in a stick package

2. the receiver does not receive packets in the buffer zone in time, resulting in receiving multiple packets.

Solution:

To avoid sticking packets, you can take the following measures. First, you can use programming settings to avoid the packet sticking problem caused by the sender. TCP provides a push command to force data to be transmitted immediately. After the TCP software receives the command, this section of data is immediately sent out, without waiting for the sending buffer to be full; the second is for the sticky packet caused by the receiver, you can optimize the program design, streamline the workload of the receiving process, and improve the priority of the receiving process so that it can receive data in a timely manner, so as to avoid the phenomenon of sticking packets. The third is controlled by the recipient, A packet of data is manually controlled and received multiple times based on the structure field, and then merged to avoid sticking to the packet.

The three measures mentioned above all have their shortcomings. The first programming setting method can avoid the sticking packet caused by the sender, But it closes the optimization algorithm, reduces the network sending efficiency and affects the application performance. It is generally not recommended. The second method can only reduce the possibility of sticking packets, but it cannot completely avoid sticking packets. When the sending frequency is high, or the network burst may cause a packet to arrive at the receiver quickly for a certain period of time, the receiver may still be too late to receive the package, resulting in sticking to the package. The third method avoids sticking packets, but the application is less efficient and is not suitable for real-time applications.

?

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.