TCP packet integrity test

Source: Internet
Author: User

ArticleSource: http://www.emlog.net/nemo/post-18.html

 

I have not edited UDPProgramBut I have seen that many lan Games use UDP. Especially for FPS with high real-time response requirements, the packet sending volume is usually small, but fast transmission and response are required. On the contrary, large online games usually use TCP. Obviously, Wan games require a higher network transmission environment.
All network programs I write are based on TCP. However, although TCP can ensure the receipt of complete data, it does not ensure that the application receives a complete packet each time it calls the Recv () function. That is to say, data of half or more packets may be received only once due to various network conditions. For example, the data received by the program three times Recv () is like this.
First time: [a] [a] [a] [a] [a] [B] [B]
Second: [B] [B] [B] [C] [C] [C]
Third time: [C] [C] [d] [d]
......
In this case, we need to perform a packet integrity check on the program. The following provides a packet integrity check pseudoCodeI was taught by a senior in the company. It is a pseudo-code, which is similar to the actual code.
// Pseudocode for packet Integrity Detection
Const int inums = 65536;
Int buflen = 0
Char buffer [inums];
Bool succrec = Fasel;

Int Len = Recv ()
Memcpy (buffer, recv_buf, Len );
Buflen + = Len;
If (buflen> package_size)
{
Memcpy (package, buffer, package_size );
Memmove (buffer, & buffer [package_size], package_size );
Buflen-= package_size;
Succrec = true;
}
If (succrec)
{
Process (Package );
}
In this case, the buffer after receiving and processing the data package for the first time is: [B] [B]
The second is: [C] [C] [C]
The third time is: [d] [d]
In this way, all three packages are complete and can be processed.

 

 

Idnemo

14: 44memcpy saves the received data, and then uses memmove to forward the pointer of the buffer. The forward position is package_size, so it should be a package_size. ??? Cai

2008-07-10 06: 22 memmove (buffer, & buffer [package_size], package_size );
Should the last parameter be changed to buflen-package_size?

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.