简体   繁体   English

如何使用协议缓冲区?

[英]How to use protocol buffers?

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 ++编写的,如果somne​​one可以帮助我使用协议缓冲区以便以下列形式交换数据,我会很高兴:

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, 谢谢你的帮助,

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...) (我猜浮动和双重实际上是不同的变量...)

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

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 your responsibility 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. 首先我们将消息大小写为32位整数(大端),然后是消息本身。 (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 message containing optional fields for all messages you want to send. See the protobuf website and mailing list archives for more info about this technique.) (另请记住,protobuf消息不是自我识别的,这意味着您需要知道要发送的消息。这通常通过包含要包含您要发送的所有消息的可选字段的包装消息来管理。请参阅protobuf网站和邮件列表存档有关此技术的更多信息。)

Are both machines x86? 两台机器都是x86吗? Otherwise you need to watch for big endian and little endian differences. 否则你需要注意big endian和little endian差异。 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 ... 在你的帖子中,所有信息都太少,无法确定是什么问题......

The answer lies in the endianess of the data being transmitted, this is something you need to consider very carefully and check. 答案在于传输数据的结尾,这是您需要仔细考虑并检查的内容。 Look here to 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. 没有这种平滑传输数据的完美措施,只是因为从unix盒发送的数据保证了windows盒上的数据在数据的内存结构方面将处于相同的顺序。 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盒上结构的填充将与windows盒上的填充不同,它归结为命令行开关的使用方式,认为结构对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM