>> ...and then sending the structure might work?
Simply sending the structure will work if:
* The structure is a POD-struct (defined below)
* The structure doesn't contain a pointer to data that you're trying to send
* (C++) The structure does not contain any members after an access specifier (public, protected, private)
* The receiving program has the same binary layout for that struct (this includes data type sizes, endianess, and structure packing.
If someone wanted to write a client using another language then they'd have to figure out the binary layout of your structures.
It's better to define your protocol in a language and architecture neutral manner. For 2 unsigned integers, you could state:
Quote:
The packet consists of two 4-byte, unsigned integers in network byte order.
To implement this, you first need to know that you're working with unsigned 4-byte integers. You can use C99's uint32_t from <stdint.h> or spin your own typedef. Then when sending the two integers, it's up to you to ensure that they are in the defined format. The only portable way to do that is to use ntohl() and send() the two integers individually.
If you have to send negative integers, then you'll also have to worry about their binary representation - 1's complement, 2's complement, sign magniture, etc...
Having said all that, you could define your protocol to be:
Quote:
Whatever my compiler turns struct A into on my PC
What's a POD-struct? [edit]Quoting the C++ standard:[/edit]
Quote:
3.9.10:
Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.
Quote:
9.0.4:
A POD-struct (the acronym POD stands for ³plain olı data²) is an aggregate class that has no non-static data members of type pointer to member, non-POD- struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type pointer to member, non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.
gg