我会通过C++套接字发送一个结构。这是一个例子的结构:序列化一个结构,并通过套接字发送它与C++
struct PIPPO
{
int x;
int y;
};
,我与使用:
PIPPO test2;
test2.x = 10;
test2.y = 20;
和我有上面的代码序列化和经由套接字发送它。问题是,如果我试图获得测试变量的HEX值,我只能看到0A,而事实上在另一台接收数据的计算机上,我无法将二进制数据转换回结构体。有人能帮我吗?
template <class T> void SerializeData(char *outputArray, T inputData)
{
memcpy(outputArray, &inputData, sizeof(inputData));
}
char *StrToHexStr(char *str)
{
char *newstr = new char[(strlen(str)*2)+1];
char *cpold = str;
char *cpnew = newstr;
while('\0' != *cpold) {
sprintf(cpnew, "%02X", (char)(*cpold++));
cpnew+=2;
}
*(cpnew) = '\0';
return(newstr);
}
char *test = new char[sizeof(PIPPO)];
memcpy((void *)&test, (void *)&test2, sizeof(test2));
send(this->m_socket, test, strlen(test), 0);
+1提供链接到`protobuf`。它看起来很有趣。 :-) – Nawaz 2011-01-31 14:40:25
我alredy看到谷歌协议缓冲区,但我认为这是一个程序,在Windows上运行,只用C++编写的真正的开销。我会推荐protobuffer异构应用程序,无论如何感谢您的回复我的问题的兴趣 – Stefano 2011-01-31 16:37:16