Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to send structs containing enum values via sockets in C
    primarykey
    data
    text
    <p>I've lots of different structs containing enum members that I have to transmit via TCP/IP. While the communication endpoints are on different operating systems (Windows XP and Linux) meaning different compilers (gcc 4.x.x and MSVC 2008) both program parts share the same header files with type declarations.</p> <p>For performance reasons, the structures should be transmitted directly (see code sample below) without expensively serializing or streaming the members inside. </p> <p>So the question is how to ensure that both compilers use the same internal memory representation for the enumeration members (i.e. both use 32-bit unsigned integers). Or if there is a better way to solve this problem...</p> <pre><code>//type and enum declaration typedef enum { A = 1, B = 2, C = 3 } eParameter; typedef enum { READY = 400, RUNNING = 401, BLOCKED = 402 FINISHED = 403 } eState; #pragma pack(push,1) typedef struct { eParameter mParameter; eState mState; int32_t miSomeValue; uint8_t miAnotherValue; ... } tStateMessage; #pragma pack(pop) //... send via socket tStateMessage msg; send(iSocketFD,(void*)(&amp;msg),sizeof(tStateMessage)); //... receive message on the other side tStateMessage msg_received; recv(iSocketFD,(void*)(&amp;msg_received),sizeof(tStateMessage)); </code></pre> <p>Additionally...</p> <ul> <li>Since both endpoints are little endian maschines, endianess is not a problem here.</li> <li>And the pack #pragma solves alignment issues satisfactorily.</li> </ul> <p>Thx for your answers, Axel</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload