简体   繁体   中英

Convert data from tcp socket in C++

As an example have the below struct, and I am sending this over a tcp socket. When I receive the data on the tcp socket, how do I reconstruct the struct information? ( how do I convert the tcp data to be printed on the console)

//data struct
typedef struct {
    int enable;
    string name;
    int numbers[5];
    float counter;
}Student;

//convert data to uint8 and send over tcp socket
uint32_t size = sizeof(student);
std:vector<std::uint8_t> data(size);
std::memcpy(data.data(), reinterpret_cast<void*>(&student),size)
sendDataOverTCPSocket(data);

Appreciate an example on how to do this. Thank you

You should not send the raw bytes like this through a.network socket. C++ provides very little guarantees about the actual memory layout of structs. If you happen to have the programs on both endpoints compiled with different compiler versions (or for different platforms) you might end up with different memory layouts. Even worse is the handling of std::string objects. Those might allocate memory dynamically on the heap and only store a pointer to that memory. Your code as is would only send the pointer and not the memory. On the receiving site you will get a segmentation fault almost for sure.

For example int might be 4 bytes on one machine and 8 bytes on another one. If you try to reconstruct the struct sent by the first machine on the second one, you will end up with corrupted invalid data.


You should do either from the following instead:

  • Write a serialization and deserialization function for your class on your own. You need to specify how long exactly each member is (use int64_t or int32_t instead of int for example) and take into account the potential for different byte ordering. On unix-like systems you could use for example these endianess conversion functions. You also must serialize the string in some way that the length can be reconstructed.
  • Use a serialization library like for example boost serialization .
  • Use a popular data exchange format like for example json. There are libraries (like JSON for Modern C++ , rapidjson or boost json that allow you to easily convert your struct to json or the other way around.

For example, you can find a live example of how to do a json serialization with boost json here .

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