user1641787
user1641787

Reputation: 21

How to send serialized data with protobuf over socket in c++

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

Answers (1)

Igor Nazarenko
Igor Nazarenko

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

Related Questions