C # SOCKET Sticky packet, packet break processing (i)

Source: Internet
Author: User

has been used in Java, about sockets, Java has a good framework MINA2, for sticky packet, broken packet processing has this good processing, the individual needs to write the code is not too much.
and C #, because not much to understand, nor to see the third-party socket framework, so have to follow the MSDN tips to achieve their own.

Before the code, let's start by talking about what happens in the process:
1, first assume that the format of the packet is as follows:
Packet Length (4 bytes) MD5 signature (32 bytes) Client ID (5 bytes) data type (5 bytes) Data ID (32 bytes) data content (dynamic length, not fixed)
|--------------------------------Baotou 78-byte--------------------------------------|------Package-----------------|

2. When receiving a packet, you may encounter problems (assuming that the buffer received is 1024 bytes each):
A, once received a complete package, that is, the complete packet length of less than 1024 bytes, and no other data packet data and the data of the packet are glued together;
That is, the data of this packet is as follows:
Buffers: |--------------------------1024 bytes----------------------|
Packet: |------Packet 1------|






B, a received data in a few packets of data bonded together, that is, the following:
Buffers: |--------------------------1024 bytes----------------------|
Packet: |------Packet 1 packet 2 packet 3 ...                Packet N------| N packet data is linked together, but not more than 1024 bytes in total

or packet: |------Packet 1 packet 2 packet 3 .....   Packet n (partial data)-----| N packet data is linked together, and the last packet of data is part of the data for this packet




C, a received data is part of a packet of data, that is, the packet's data length is greater than 1024 bytes, that is, the following:
Buffers: |--------------------------1024 bytes----------------------|
Packet 1:|-------------data 1 (partial data for a packet)----------------| Some data for a packet
Packet 2:|-------------Data 2 (partial data for a packet)----------------| Some data for a packet
..........
Packet n:|-------------data n (partial data for a packet)--------| The last piece of data for a packet, and then the data with no other packets sticking together




D, a received data is part of a packet of data, that is, the packet's data length is greater than 1024 bytes, that is, the following:
Buffers: |--------------------------1024 bytes----------------------|
Packet 1:|-------------data 1 (partial data for a packet)----------------| Some data for a packet
Packet 2:|-------------Data 2 (partial data for a packet)----------------| Some data for a packet
..........
Packet n:|-------------Data N (part of a packet of data) the data of another package--| The last piece of data for a packet, followed by data from other packets, glued together




3, processing method:
The first time you receive, you can certainly receive information that includes the length of the current packet, by parsing and restoring this information, you can compare the length of the packet with the length of the buffer, to see if it belongs to the above (2nd) type, according to the different types, the corresponding processing:


A, type A, this situation is the simplest processing, according to the length of the packet information, receive and parse the data can be


b, type B, this situation scored two cases, but first of all get the buffer data, and then
1) When n packets are not longer than 1024 bytes:
By recursion, it's good to parse each packet's information according to the length of each packet.


2) When the last part of the buffer data also has some data of a package (temporarily: Data packet x part of the data A1), then scored two cases processed:

I, data packet x part of the data A1 length is greater than or equal to 4 bytes, also includes the length of the packet x information, and save this part of the data A1, and get the length of the packet x
The next time you receive it, you need to glue the data you just saved to the data you received, and then process it based on the length you obtained earlier.


II, the data packet x part of the data A1 length is less than 4 bytes, then first save this part of the data A1
The next time you receive it, you need to glue the data you just saved with the data you received and parse the length of the packet, and then handle it according to that length.

Of course, this i,ii two types, can be installed II this way to handle


c, type C, this situation, need to record two information, one is the length of the packet, and the other is the packet data.
1) first obtain the first part of the packet data, save, and then parse out the length of the package
2) Continue to receive data and glue the previously saved data until all data received by this packet is completed, and then parse the packet


D, type D, the case is a mixture of type C and type B
1) The data is saved according to the Type C, and then the packet is parsed according to the length of the packet.
2) Then, depending on the case of type B, receive the data from the last part of the buffer, which is part of the next packet, and then process it according to the case of type B

C # SOCKET Sticky packet, packet break processing (i)

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.