How to send data from a server to a client in embedded messages?

58 views
Skip to first unread message

Prasanna B

unread,
Oct 28, 2021, 11:13:20 PM10/28/21
to Protocol Buffers
Hello all,
I am implementing a simple client-server grpc-c++ based application. In the Hello rpc, I am taking the request and sending the fields of another message called SeverInfo as response. The problem is I exactly don't know how to send this ServerInfo data to a client from server side. We basically use set_fieldname(ex: set_name) for general datatypes to send the data but how should we send this serverInfo data to HelloResponse and then to HelloRequest. Can somebody please help me??


syntax = "proto3";

package recognizer;

service Recognizer {
   rpc Hello(HelloRequest) returns (HelloResponse){}
}
message HelloRequest {
    string name = 1;
}
message HelloResponse {
    ServerInfo server = 1;
}
message ServerInfo {
    string name = 1;
    string os = 2;
    string arch = 3;
    int32 nproc = 4;
}

Prasanna B

unread,
Nov 5, 2021, 1:03:04 AM11/5/21
to Protocol Buffers
I got the answer. I am sharing the answer here so that anyone having this problem can use this. Thank you.

First solution is change the rpc call from 
rpc Hello(HelloRequest) returns (HelloResponse){}    to   rpc Hello(HelloRequest) returns (ServerInfo){}. This will work.

Second solution is when we have an obligation to not to change the rpc call then in server we can use the below code to call the ServerInfo fields and assign them to HelloResponse.

ServerInfo* serverinfo=new ServerInfo();
serverinfo->set_name("");
serverinfo->set_os("");
serverinfo->set_arch("");
serverinfo->set_nproc("");
HelloResponse* res;
res->set_allocated_server(serverinfo);
Reply all
Reply to author
Forward
0 new messages