如何使用协议缓冲区?

发布于 2024-08-13 01:45:37 字数 331 浏览 1 评论 0原文

有人可以帮忙告诉我如何使用协议缓冲区吗?实际上,我想通过套接字在unix上运行的程序和windows上运行的另一个程序之间交换数据,以便进行模拟研究。

使用套接字交换数据的程序是用 C/C++ 编写的,如果 somne​​one 可以帮助我使用协议缓冲区以便以以下形式交换数据,我会很高兴:

struct snd_data{
    char *var="temp";
    int var1=1;
    float var2;
    double var2;
}

我尝试了几种方法,但仍然没有交换数据正确。任何帮助将不胜感激

感谢您的帮助,

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.

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 :

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乖不如嘢 2024-08-20 01:45:37

首先在 .proto 文件中定义消息:(

package foo;

message snd_data {
  required string var= 1;
  required int32 var1 = 2;
  optional float var2 = 3;
  optional double var3 = 4;
}

我猜 float 和 double 实际上是不同的变量...)

然后使用 protoc 编译它,然后就有了实现缓冲区的代码。

有关详细信息,请参阅:http://code.google.com/apis/protocolbuffers/文档/cpptutorial.html

You start by defining your message in a .proto file:

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.

For further information see: http://code.google.com/apis/protocolbuffers/docs/cpptutorial.html

兮颜 2024-08-20 01:45:37

您如何将消息写入套接字? Protobufs 本身不是字节序敏感的,但 protobufs 也没有定义传输机制——protobuf 定义了消息与其序列化形式(这是一个(8 位)字节的序列)之间的映射,并且它是你的< /strong> 负责将此字节序列传输到远程主机。

在我们的例子中,我们定义了一个非常简单的传输协议;首先,我们将消息大小写为 32 位整数(大端),然后是消息本身。 (另请记住,protobuf 消息不是自我识别的,这意味着您需要知道您正在发送哪条消息。这通常是通过使用包含您想要发送的所有消息的可选字段的包装消息来管理的。有关此技术的更多信息,请参阅 protobuf 网站和邮件列表档案。)

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.

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 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.)

苹果你个爱泡泡 2024-08-20 01:45:37

两台机器都是x86吗?否则,您需要注意大端和小端的差异。结构打包也值得关注。由于指针在不同平台上的大小不同,因此传递指针也可能存在问题。总之,您的帖子中的信息太少,无法确定到底出了什么问题......

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 ...

第七度阳光i 2024-08-20 01:45:37

答案在于传输数据的字节序,这是您需要仔细考虑和检查的事情。请查看此处,了解字节顺序的作用以及导致数据在接收方和接收方上混乱的情况。发件人。没有这样完美的平滑传输数据的措施,只是因为从unix机器发送的数据保证了windows机器上的数据在数据的内存结构方面具有相同的顺序。此外,unix 盒子上结构的填充将与 windows 盒子上的填充不同,这归结为如何使用命令行开关,想想结构对齐。

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文