我想通过protobuf序列化一些数据并通过套接字(winsock2)发送它,但wathever我做,我做不到。如果有人能给我一个简单的例子说明如何去做,我会非常感谢。我已经检查过Google的文档,但没有有用的例子,对于初学者来说我的解释是不可理解的。 在此先感谢您的帮助!那么我现在要做的是:如何发送序列化的数据与protobuf通过套接字在c + +
客户端:
printf("Sporočilo: ");
getline(cin, line);
if(line == "exit") break;
printf("ID odjemalca: ");
cin >> id;
message::Message sporocilo;
sporocilo.set_bodytext(line);
sporocilo.set_uniqueid(id);
//... some usefull code for serializing data and send it over socket
send(sClient, Message, sizeof(Message), 0);
服务器端:
WSARecv(Socket, &(DataBuf), 1, &RecvBytes, &Flags, NULL, NULL);
//... some usefull code for deserializing data and getting out bodytext and uniqueid
cout << sporocilo.bodytext();
cout << sporocilo.uniqueid();
你的问题是什么?当你运行你的代码时会发生什么? –
那么,问题是我不明白如何使用protobuf序列化数据并将其发送到套接字。我尝试了一些代码,但在服务器端我收到消息:libprotobuf错误c:\ temp \ protobuf-2.4.1 \ src \ google \ protobuf \ message_lite.cc:123 ]无法解析类型为“消息的消息.Message“,因为它缺少必填字段:uniqueid,bodytext;如果有人可以在客户端和服务器端写下这几行代码,并向我解释它是如何工作的,我真的很惊讶! – user1641787
message :: Message的定义在哪里? –