Send struct over socket in C

后端 未结 2 1377
不知归路
不知归路 2020-12-18 02:22

I am developing a client/server program and my client has to send messages to the server.

Sample message C structure:

struct Reg         


        
相关标签:
2条回答
  • 2020-12-18 02:37

    While the code will work if you send and recv on the same endian machine - your client and server should ideally perform a host to network endian conversion to maintain coherence when inspecting clientPid.

    0 讨论(0)
  • 2020-12-18 02:49

    sizeof(regn) gives size of your complete structure Registration, wheras sizeof(data) is size of pointer on your machine that is 4 bytes (data should be pointer of Registration type).

    In expression:

    memcpy(data, &regn, sizeof(regn));
            ^     ^
            |     value variable of struct type 
            is pointer
    

    also notice in printf, . is used to access elements e.g. regn.multicastGroup.

    0 讨论(0)
提交回复
热议问题