C and C # socket cross-platform communication and transmission structure,
Recently, I need to write a software program consisting of C for interaction between the server and the C # client. At the beginning, I found that a fault occurred during C # parsing, after careful research, it is found that the sender transmission is too fast, resulting in a so-called sticking packet. That is, in C #, Receive (). This function returns data connected by multiple structs, which of course cannot be parsed. My solution is as follows:
List <byte []> listb = new List <byte []> (); ..........int recv = newclient. receive (b1); for (int x = 0; x <recv/12; x ++) {byte [] b2 = new byte [12]; int I = 0; for (int lc = x * 12; lc <x * 12 + 12; lc ++) {b2 [I] = b1 [lc]; I ++;} listb. add (b2 );}
In this way, you can directly parse the elements in listb, directly
Foreach (var I in listb ){................}
However, there was still a problem in subsequent parsing. The second half of my struct element was not transmitted by the sender or garbled. Later, I checked carefully and found that because the SERVER side is written in C, the CLIENT is written in C #, and the two sides are in different environments. When C declares a struct, the so-called memory alignment occurs. In this way, a portion of the binary is a number generated by the compiler for filling in the conversion. My solution is to fill it with 0 bytes (PS. I finally know why the Protocol format of RIP is so strange ). After filling, the size of the struct remains unchanged, and the C # side is successfully parsed.