C and C # socket cross-platform communication and transmission structure,

Source: Internet
Author: User

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.

Related Article

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.