C++ 如何使用协议缓冲区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1834434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use protocol buffers?
提问by make
Could someone please help and tell me how to use protocol buffers. Actually I want to exchange data through sockets between a program running on unix and anoother running on windows in order to run simulation studies.
有人可以帮忙告诉我如何使用协议缓冲区。实际上,我想通过套接字在 unix 上运行的程序和 Windows 上运行的另一个程序之间交换数据,以便运行模拟研究。
The programs that use sockets to exchange data, are written in C/C++ and I would be glad if somneone could help me to use protocol buffers in order to exchange data in the form of :
使用套接字交换数据的程序是用 C/C++ 编写的,如果有人可以帮助我使用协议缓冲区以以下形式交换数据,我会很高兴:
struct snd_data{
char *var="temp";
int var1=1;
float var2;
double var2;
}
I tried several ways, but still data are not exchanged correctly. Any help would be very appreciated
我尝试了几种方法,但仍然无法正确交换数据。任何帮助将不胜感激
Thanks for your help,
谢谢你的帮助,
回答by Douglas Leeder
You start by defining your message in a .proto file:
您首先在 .proto 文件中定义您的消息:
package foo;
message snd_data {
required string var= 1;
required int32 var1 = 2;
optional float var2 = 3;
optional double var3 = 4;
}
(I guess the float and double actually are different variables...)
(我猜 float 和 double 实际上是不同的变量......)
Then you compile it using protoc
and then you have code implementing your buffer.
然后你使用编译它protoc
,然后你有代码实现你的缓冲区。
For further information see: http://code.google.com/apis/protocolbuffers/docs/cpptutorial.html
有关更多信息,请参阅:http: //code.google.com/apis/protocolbuffers/docs/cpptutorial.html
回答by JesperE
How are you writing your messages to the socket? Protobufs is not endian-sensitive itself, but neither does protobufs define a transport mechanism -- protobuf defines a mapping between a message and its serialized form (which is a sequence of (8-bit) bytes) and it is yourresponsibility to transfer this sequence of bytes to the remote host.
您如何将消息写入套接字?Protobufs 本身不是对字节序敏感的,但 protobufs 也没有定义传输机制——protobuf 定义了消息与其序列化形式(这是(8 位)字节序列)之间的映射,您有责任传输它到远程主机的字节序列。
In our case, we define a very simple transport protocol; first we write the message size as an 32-bit integer (big endian), then comes the message itself. (Also remember that protobuf messages are not self-identifying, which means that you need to know which message you are sending. This is typically managed by having a wrapper messagecontaining optional fields for all messages you want to send. See the protobuf website and mailing list archives for more info about this technique.)
在我们的例子中,我们定义了一个非常简单的传输协议;首先我们将消息大小写为 32 位整数(大端),然后是消息本身。(还要记住,protobuf 消息不是自我识别的,这意味着您需要知道您发送的是哪条消息。这通常是通过一个包装消息来管理的,其中包含您要发送的所有消息的可选字段。请参阅 protobuf 网站和有关此技术的更多信息,请参阅邮件列表档案。)
回答by leesei
Endianess is handled within protobuf.
Endianess 在 protobuf 中处理。
See:
看:
https://groups.google.com/forum/?fromgroups#!topic/protobuf/XbzBwCj4yL8
https://groups.google.com/forum/?fromgroups#!topic/protobuf/XbzBwCj4yL8
How cross-platform is Google's Protocol Buffer's handling of floating-point types in practice?
回答by Goz
Are both machines x86? Otherwise you need to watch for big endian and little endian differences. Its also worth paying attention to struct packing. Passing pointer can be problematic too due to the fact pointer are different sizes on different platforms. All in there is far too little information in your post to say, for certain, what is going wrong ...
两台机器都是x86吗?否则,您需要注意大端和小端的差异。结构体包装也值得关注。由于指针在不同平台上的大小不同,传递指针也可能存在问题。总而言之,您帖子中的信息太少了,无法肯定地说出了什么问题......
回答by t0mm13b
The answer lies in the endianess of the data being transmitted, this is something you need to consider very carefully and check. Look hereto show what endianness can do and cause data to get messed up on both the receiver and sender. There is no such perfect measure of transferring data smoothly, just because data sent from a unix box guarantees the data on the windows box will be in the same order in terms of memory structure for the data. Also the padding of the structure on the unix box will be different to the padding on the windows box, it boils down to how the command line switches are used, think structure alignment.
答案在于传输数据的字节序,这是您需要非常仔细地考虑和检查的事情。查看此处以显示字节序可以做什么,并导致数据在接收方和发送方上都变得混乱。没有这么完美的方法来平滑传输数据,只是因为从unix box发送的数据保证windows box上的数据在数据的内存结构方面是相同的。此外,unix 框上的结构填充将与 windows 框上的填充不同,归结为命令行开关的使用方式,考虑结构对齐。