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?