Reputation: 21
I'm trying to serialize some data with protobuf and send it over a socket (winsock2). I would really appreciate a simple example on how to do it. I have already checked the Google documentation, but there are no useful examples or beginner explanations.
Thanks in advance for some help! What I'm trying to do is:
Client side:
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);
Server side:
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();
Upvotes: 2
Views: 6554
Reputation: 2264
The easiest way to serialize is simply:
string buffer;
sporocilo.AppendToString(&buffer);
send(sock, buffer.c_str(), buffer.size(), 0);
It's hard to say if you are doing something wrong on the receive side without the code before and after WSARecv that you didn't include.
Upvotes: 3