我有一个test.proto文件,其代码如下所示。我在我的客户端服务器程序中使用这个文件生成的代码。
message Person {
required string user_name = 1;
optional int32 favourite_number = 2;
repeated string interests = 3;}
客户端我发送数据没有问题,但在服务器端,我得到协议缓冲区解析错误(在file:\protobuf\message_lite.cc(Line123)中)说“不能解析'person‘类型的消息,因为它缺少必需的field: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 :(有了看这里,但仍然不能从那个解释中得到它,希望有人能解决它。
谢谢!
发布于 2014-01-30 19:25:19
谷歌::protobuf::io::ArrayInputStream(缓冲器,filldata.ByteSize());
此时,filldata是一个新初始化的消息,因此filldata.ByteSize()为零。所以,您要告诉protobufs解析一个空数组。因此,没有设置任何字段,您将得到一个必需的字段错误。消息的长度是可变的,因此您需要确保消息大小是从服务器传递的。
https://stackoverflow.com/questions/21435924
复制相似问题