TCP and UDP "PROTECT message boundary" (Classic)

Source: Internet
Author: User

In Socket network programs, TCP and UDP are connection-oriented and non-connection-oriented respectively. Therefore, for TCP socket programming, both the sending and receiving ends (the client and the server) must have one pair of sockets. Therefore, in order to send multiple packets to the receiving end, the Nagle algorithm is used to send data to the other Party more effectively. Data with a small interval and a small amount of data is merged into a large data block and then packed. In this way, it is difficult for the receiver to distinguish, and a scientific package splitting mechanism must be provided.
For UDP, the block merge optimization algorithm is not used. In this way, we currently think that UDP supports one-to-many mode, so the receiver's skbuff (socket buffer) the chain structure is used to record each incoming UDP packet, and each UDP packet has a message header (message source address, port, and other information). In this way, for the receiving end, it's easy to differentiate.

PROTECT message boundaries and streams

So what is to protect the message boundary and stream?

Message boundary protection means that the transmission protocol treats data as an independent message on the Internet.
Transmission. The receiving end can only receive independent messages. That is to say, there is a message boundary to protect and receive messages.
The sender can only receive one packet sent by the sender at a time.
Stream-oriented means no protection.Message Protection BoundaryIf the sender continuously sends data,

The acceptor may receive two or more packets in one receiving action.

For example, we send three data packets in a row with a size of 2 kb,
4 K, 8 K, all three packets have reached the network stack of the acceptor.
UseUDPProtocol, no matter how much buffer we use to receive data, we must have

Only when three Receiving actions are completed can all data packets be received.
As long as we set the size of the received buffer to more than 14 K, we can set all
The data packet is received only once.

This is becauseUDPThe Protocol protects message boundaries so that each message is independent.

Streaming transfers data as a series of data streams. He does not consider data as a message.

Therefore, when using TCP communication, it is unclear that TCP is stream-based.
Transmission: when data is continuously sent, they often know that TCP packet loss occurs. Otherwise,
Because when they use a large enough buffer, they may receive two
To more data packets, and many people tend to ignore this point, only the first
But other received data packets are ignored.
You must pay attention to this when programming the network of the class.

Conclusion:
According to the above, we can understand that TCP, in order to ensure reliable transmission, should minimize additional
Overhead (verification is required for each packet sending). Therefore, stream-oriented transmission is adopted,
Compared with message-oriented transmission, the number of sent packets can be reduced. This reduces the number of additional
Sales. However, for programs with frequent data transmission, using TCP may easily stick packets.
Of course, for the receiver program, if the machine load is heavy, it will also be in the receiving buffer
Stick the package. In this way, extra package splitting is required at the receiving end, increasing the workload. Therefore, this feature
It is not suitable for scenarios where data requires reliable transmission, but it does not need to be transmitted too frequently (
The interval between two operations is 100 ms, which is determined by the tcp wait interval, depending on the kernel
In)

UDP, because it is oriented to message transmission, it mounts all received messages to the buffer.
Therefore, it is more convenient for data extraction and separation. However,
It does not have a mechanism to stick packets. Therefore, when a small amount of data is sent, data packets will occur.
If the payload is small, the system sending overhead (system call,
Write hardware, etc.) and receive overhead. Therefore, it is best to set a suitable data packet
To send UDP data. (UDP has a maximum load of 1472, so it is best
The amount of data transmitted each time is close to this number, which is particularly suitable for large volumes of data such as video and audio.
At the same time, reduce handshakes to ensure real-time Streaming Media)

From:
Http://hi.baidu.com/chongerfeia/blog/item/b1e572f631dd7e28bd310965.html

Solution to TCP unprotected message Boundary
There are generally three solutions to this problem:

(1) Send a fixed-length message

(2) send the message size together with the message size.

(3) use special tags to differentiate message intervals

The following describes the first two methods:

1. Send a fixed-length message
The advantage of this method is that it is very easy, and as long as the length of the good news is specified, no unsent data is missing, we override a sendmessage method. The Code is as follows:

Private Static int sendmessage (socket S, byte [] MSG)

{
Int offset = 0;
Int size = msg. length;
Int dataleft = size;

While (dataleft> 0)
{

Int sent = S. Send (MSG, offset, socketflags. None );
Offset + = sent;
Dataleft-= sent;

}

Return offset;
}

Brief Analysis of this function: the parameter S is the socket for communication, and MSG is the byte array to be sent. This method uses the while loop to check whether there is still data not sent. Especially when a large data packet is sent, the function is more obvious if it cannot be completely sent at a time. In particular, sent is used to record the actual amount of data sent, which is the same as Recv and returns the actual total number of data sent.

After the sentmessage function is available, you must design a new recive Method Based on the specified message length. The Code is as follows:

Private byte [] recivemessage (socket S, int size)
{

Int offset = 0;
Int Recv;
Int dataleft = size;
Byte [] MSG = new byte [size];

While (dataleft> 0)

{

// Receives the message
Recv = S. Receive (MSG, offset, dataleft, 0 );
If (Recv = 0)

{

Break;

}
Offset + = Recv;
Dataleft-= Recv;

}

Return MSG;

}

The preceding method is applicable when the message length is not long.

2. The message length is sent together with the message

We can do this: the actual size of the message is indicated by the integer value of the message, so we need to convert the integer number to the byte type. The following is the sendmessage Method for sending a variable-length message. The Code is as follows:

Private Static int sendmessage (socket S, byte [] MSG)
{

Int offset = 0;
Int sent;
Int size = msg. length;
Int dataleft = size;
Byte [] msgsize = new byte [2];

// Convert the Message Size from integer to the byte type that can be sent
Msgsize = bitconverter. getbytes (size );

// Message sending Length
Sent = S. Send (size );

While (dataleft> 0)

{

Sent = S. Send (MSG, offset, dataleft, socketflags. None );

// Set the offset

Offset + = sent;
Dataleft-= sent;

}

Return offset;

}

The following is the recivevarmessage Method for receiving variable-length messages. The Code is as follows:

Private byte [] recivevarmessage (socket S)
{

Int offset = 0;
Int Recv;
Byte [] msgsize = new byte [2];

// Convert the message length of the byte array into an integer
Int size = bitconverter. toint16 (msgsize );
Int dataleft = size;
Byte [] MSG = new byte [size];

// Receive length information of 2 bytes
Recv = S. Receive (msgsize, 0, 2, 0 );
While (dataleft> 0)
{

// Receive data
Recv = S. Receive (MSG, offset, dataleft, 0 );
If (Recv = 0)
{
Break;
}
Offset + = Recv;
Dataleft-= Recv;

}

Return MSG;

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xinshi9608/archive/2010/12/31/6109511.aspx

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.