简体   繁体   English

通过TCP套接字编码和发送XDR结构(C-Linux)

[英]Encode and send an XDR struct over a TCP socket (C - Linux)

I have to ask the question below: 我必须问以下问题:

I have to send the following XDR message: 我必须发送以下XDR消息:

struct Responde{
 float numbers<>;
 bool end;
}

I know how to enconde array (xdr_array) and bool (xdr_bool) but how can I: 我知道如何对数组(xdr_array)和布尔(xdr_bool)进行编码,但是我该如何:

1) Encode the struct? 1)编码结构? 2) Send the struct over a socket? 2)通过套接字发送结构?

Thanks to all, but I don't know XDR and I have to use it. 感谢所有人,但是我不了解XDR,因此必须使用它。 Thanks again. 再次感谢。

Nb: How title shows I refer to C language and Linux environment. Nb:标题显示方式我指的是C语言和Linux环境。

The most flexible solution would of course be to use the rpcgen tool to compile the XDR definition to C code, which you can then compile and used in your C application. 当然,最灵活的解决方案是使用rpcgen工具将XDR定义编译为C代码,然后可以对其进行编译并在C应用程序中使用。 But if you do not want introduce this dependency into your project, you can of course write the serialization code by hand. 但是,如果您不想在项目中引入这种依赖性,则当然可以手动编写序列化代码。

RFC 1831 will give you a detailed description of how such a structure is serialize but here is what you need to know for your example: RFC 1831将为您提供有关如何序列化此结构的详细说明,但这是您需要为示例了解的内容:

  • All number in XDR are encoded in big-endian order. XDR中的所有数字均按大端顺序编码。
  • All fields in a structure are aligned to 4 bytes and the fields are concatenated in the order they are declared. 结构中的所有字段都对齐为4个字节,并且这些字段按照声明的顺序串联在一起。
  • A float encodes a number as a 32-bit floating point number. float将数字编码为32位浮点数。
  • The variable length array of floats is encoded as a 32-bit integer with containing the number of elements followed by the float numbers. 浮点数的可变长度数组被编码为32位整数,其中包含元素数量,后跟浮点数。
  • A bool encodes a boolean value as a 32-bit integer where a true value is encoded as the number 1 and a false value is encoded as the number 0 . bool将布尔值编码为32位整数,其中,将true值编码为数字1 ,将false值编码为数字0

For example, if your struct was filled with 3 floats and the value true, you would get the following byte stream: 例如,如果您的结构填充了3个浮点,并且值为true,则将获得以下字节流:

00 00 00 03  xx xx xx xx  yy yy yy yy  zz zz zz zz  00 00 00 01

Where the sequences of x , y and z are the floating point numbers. 其中xyz的序列是浮点数。

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

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