简体   繁体   中英

send data struct over network between C/C++ server, C/C++ and C# clients

My client will communicate with clients using a struct/class called annoucement. I suppose I will write the server in C++. There will be many different classes that inherit annoucement. My problem is to send these classes over network to a client

I thought maybe I should use iovec. However I don't know if there is anything corresponding to iovec in C#.

The idea is: I would send a struct with writev, a client would use readv and read the data from the socket like it would be an annoucement class, check uint16 variable in the received struct to deduce the exact type and read the rest of data with apprioprite iovec/readv.

C# can send whole objects as an array but how to interpret it in C/C++?

I want both C/C++ and C# clients to work with the same protocol but that is the first time I designed the protocol and I'm not sure all my ideas will work together.

Any hint'd be appreciated.

Don't try to define your protocol in terms of structs.

Define the format of an announcement, ie what bytes in what order constitute an announcement, and then define how multiple announcements are transmitted in sequence.

Once you've got this wire format defined, you can implement parsers and writers in each programming language, using the most appropriate programming language constructs, libraries and frameworks available in each.

A good example for this is protobuf : format specification , C++, Java, Python implementation , .NET implementation .

The question is a bit chaotic, but I have a feeling that you might be interested in Protocol Buffers for efficiently serializing data and transferring it over the network.

There are a number of options when it comes to serialization. This FAQ at parashift.com discusses serialization/deserialization in detail: http://www.parashift.com/c++-faq-lite/serialization.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM