假设我有一个结构,其成员值我想通过网络发送到另一个使用winsock 2的系统。我正在使用C ++语言。 我如何将其转换为char *记住结构必须在发送之前被序列化,以及如何将char *反序列化为另一端的struct?我发现boost序列化是对类似问题的建议,但任何人都可以用一个小的代码片段来说明序列化和反序列化吗?
这个问题可能看起来很基本,但相关帖子的其他答案并没有多大帮助。
答案 0 :(得分:16)
以下示例显示了将struct
序列化为char
数组并对其进行反序列化的最简单方法。
#include <iostream>
#include <cstring>
#define BUFSIZE 512
#define PACKETSIZE sizeof(MSG)
using namespace std;
typedef struct MSG
{
int type;
int priority;
int sender;
char message[BUFSIZE];
}MSG;
void serialize(MSG* msgPacket, char *data);
void deserialize(char *data, MSG* msgPacket);
void printMsg(MSG* msgPacket);
int main()
{
MSG* newMsg = new MSG;
newMsg->type = 1;
newMsg->priority = 9;
newMsg->sender = 2;
strcpy(newMsg->message, "hello from server\0");
printMsg(newMsg);
char data[PACKETSIZE];
serialize(newMsg, data);
MSG* temp = new MSG;
deserialize(data, temp);
printMsg(temp);
return 0;
}
void serialize(MSG* msgPacket, char *data)
{
int *q = (int*)data;
*q = msgPacket->type; q++;
*q = msgPacket->priority; q++;
*q = msgPacket->sender; q++;
char *p = (char*)q;
int i = 0;
while (i < BUFSIZE)
{
*p = msgPacket->message[i];
p++;
i++;
}
}
void deserialize(char *data, MSG* msgPacket)
{
int *q = (int*)data;
msgPacket->type = *q; q++;
msgPacket->priority = *q; q++;
msgPacket->sender = *q; q++;
char *p = (char*)q;
int i = 0;
while (i < BUFSIZE)
{
msgPacket->message[i] = *p;
p++;
i++;
}
}
void printMsg(MSG* msgPacket)
{
cout << msgPacket->type << endl;
cout << msgPacket->priority << endl;
cout << msgPacket->sender << endl;
cout << msgPacket->message << endl;
}
答案 1 :(得分:4)
你可以做到
struct MyStruct {
int data;
char* someNullTerminatedName; // Assuming not larger than 1023 chars
std::ostream& serialize(std::ostream& os) const {
char null = '\0';
os.write((char*)&data, sizeof(data));
os.write(someNullTerminatedName, strlen(someNullTerminatedName));
os.write(&null, 1);
return os;
}
std::istream& deserialize(std::istream& is) {
char buffer[1024];
int i = 0;
is.read((char*)&data, sizeof(data));
do { buffer[i] = is.get(); ++i; } while(buffer[i] != '\0');
if (someNullTerminatedName != NULL) free(someNullTerminatedName);
someNullTerminatedName = (char*)malloc(i);
for (i = 0; buffer[i] != '\0'; ++i) {
someNullTerminatedName[i] = buffer[i];
}
return is;
}
};
由您来处理int
及其他内容的字节顺序和差异。
示例:
MyStruct foo, bar;
std::stringstream stream;
foo.serialize(stream);
// ... Now stream.str().c_str() contains a char* buffer representation of foo.
// For example it might contain [ 1f 3a 4d 10 h e l l o w o r l d \0 ]
bar.deserialize(stream);
// ... Now bar is a copy, via a serial stream of data, of foo.
如果您有一个通过C ++ iostream暴露其界面的套接字库,那么您甚至不需要字符串流。
答案 2 :(得分:4)
您还可以查看Google的Protocol Buffers,它是一个独立于平台/语言的库,用于在主机之间发送数据。
然而,范式转向首先编写协议,然后将数据结构拟合到其中。这样做的好处是它可以强制您的软件架构适合简单的数据类型。
答案 3 :(得分:1)
好的,我会从提升网站上取example,因为我不明白你无法理解的内容。
我添加了一些注释和更改,以便您可以通过网络进行传输。网络代码本身不在此处。为此,您可以查看boost::asio。
int main() {
// create and open a character archive for output
// we simply use std::strinstream here
std::stringstream ofs;
// create class instance
const gps_position g(35, 59, 24.567f);
// save data to archive
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g;
// archive and stream closed when destructors are called
}
// now we have const char* ofs.str().c_str()
// transfer those bytes via network
// read them on the other machine
gps_position newg;
{
// create and open an archive for input
std::stringstream ifs(the_string_we_read_from_the_network);
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
// archive and stream closed when destructors are called
}
return 0;
}
答案 4 :(得分:0)
如果您的结构是POD
,则可以使用memcpy
:
::memcpy(data, &your_struct, sizeof(YourStruct)));
在接待处反之亦然:
::memcpy(&your_struct, data, sizeof(YourStruct)));
data
是char*
的位置。不要忘记你必须分配它,确保它是最大的并且最后删除它。