使用C API在协议缓冲区中解析误差
Parsing error in Protocol Buffer using C++ API
我有一个test.proto文件,其中所示的代码。我正在我的客户端服务器程序中使用此文件生成的代码。
message Person {
required string user_name = 1;
optional int32 favourite_number = 2;
repeated string interests = 3;
}
客户端我没有问题发送数据,但是在服务器端,我正在获得协议缓冲区解析错误(在文件中: protobuf message_lite.cc(line123))说" cant of type'cant它缺少所需字段: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 :(
在这里看了看,但仍然无法从该解释中得到它,希望有人可以修复它。
谢谢!
google::protobuf::io::ArrayInputStream ais(buffer,filldata.ByteSize());
此时,filldata
是一条新的消息,因此filldata.ByteSize()
为零。因此,您正在告诉Protobufs解析一个空数组。因此,没有设置字段,您会收到所需的字段错误。消息的长度可变,因此您需要确保从服务器传递确切的消息大小。
相关文章:
- C++字符*缓冲区的大小
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 为什么在浮点中从大到小会引入更多的误差
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- ostream过载时的缓冲区冲洗
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- Xaudio2在更改缓冲区或循环时弹出声音
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 多线程双缓冲区
- Android P-9.0.0_r53 Logcat主缓冲区超出定义大小
- 套接字读取后,我在缓冲区中看到意外输入
- std::带有自定义缓冲区的 iostream 不允许我写入
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- OpenGL 16 位模板缓冲区?
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 在 openGL 中多次绑定缓冲区
- 使用C API在协议缓冲区中解析误差