2014-01-29 62 views
0

我有一个test.proto文件,其代码如下所示。我正在使用我的客户端服务器程序中的此文件生成的代码。使用C++ API解析协议缓冲区中的错误

message Person { 
required string user_name  = 1; 
optional int32 favourite_number = 2; 
repeated string interests  = 3; 

}

客户端我没有问题,发送数据,但在服务器端我收到的协议缓存解析错误(该文件中:\ protobuf的\ message_lite.cc(line123))说“不能解析消息的类型'人',因为它缺少必填字段:user_name“

虽然我已检查我的客户端,但无法找到任何错误,但我可能会丢失一些不读取字符串数据的服务器端?

   //Server side code for Protocol Buffers 
       Person filldata; 
     google::protobuf::uint32 size; 
       //here i might need google::protobuf::string stsize; Not sure ? 
     google::protobuf::io::ArrayInputStream ais(buffer,filldata.ByteSize()); 
     CodedInputStream coded_input(&ais); 
     coded_input.ReadVarint32(&size); 
       //have tried here both coded_input.ReadString and coded_input.ReadRaw 
     filldata.ParseFromCodedStream(&coded_input); 

     cout<<"Message is "<<filldata.DebugString(); 
       //still getting same error have no idea what to do exactly to fix it :(

Haved Looked Here但仍想不出从该解释得到了它,希望有人能解决这个问题。

谢谢!

回答

1
google::protobuf::io::ArrayInputStream ais(buffer,filldata.ByteSize()); 

此时,filldata是一个新初始化的信息,所以filldata.ByteSize()为零。所以,你告诉protobufs解析一个空数组。因此,没有设置字段,并且您会收到必填字段错误。消息具有可变长度,因此您需要确保从服务器传递确切的消息大小。

+0

感谢它的工作原理,现在我使用服务器端缓冲区接收的字节。 – User