Serialization issues while sending struct over socket

后端 未结 1 578
庸人自扰
庸人自扰 2021-01-22 15:28

I am developing a Client/Server based on UDP I want to send different messages to the client from the server. There are different C structures defined for each message.

1条回答
  •  轮回少年
    2021-01-22 16:25

    OP has 2 problems in serializeTask()

    for(int i=0; icCnt; i++)
      msg = serialize_int(msg,t->cId[i*4]); [i*4]
    ...
    for(int i=0; idata[i]); strlen(data)
    

    Should be (assuming i should have been idata)

    for(int i=0; icCnt; i++)
      msg = serialize_int(msg,t->cId[i]);  // [i]
    ...
    for(int i=0; idata); i++)   // strlen(data) + 1
      msg = serialize_char(msg,t->data[i]);
    

    The first for loop serialize every 4th cId[]. OP certainly wanted to serialize consecutive cId[].
    Only the length of the data string was serialized. OP certainly wanted to serialize all that and a NUL terminating byte.


    The data in the posted buffer is more likely the below, which does not match the serialization code. This implies the higher level code populating Task* t is wrong. I am confident that the values seen in fields mType and tkType are either pointers or float, again Task* t is likely amiss before the serialization.

    0xb6700300 or -3.576453e-06
    0xb6700388 or -3.576484e-06
    0xb6700568 or -3.576593e-06
    0xb6700568 or -3.576593e-06
    0x000000 or 0.000000e+00
    0x000000 or 0.000000e+00
    0xb67005a8 or -3.576608e-06
    0xb67005ac or -3.576609e-06
    0xb67005b4 or -3.576611e-06
    0xb67005c9 or -3.576615e-06
    0xb67005de or -3.576620e-06
    0xb67005e6 or -3.576622e-06
    0xb67005ee or -3.576624e-06
    0xb67005fb or -3.576627e-06
    def\0cohorts\0pending_assign_tasks\0pending_assign_tasks\0message\0message\0?\0
    ...
    

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