在sendto()函数中发送一个结构体——C语言 [英] Sending a structure in the sendto() function - C language

查看:131
本文介绍了在sendto()函数中发送一个结构体——C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试使用 sendto() 函数向客户端发送消息.

So I am trying to send a message to a client using the sendto() function.

sendto() 函数的原型是这样的:

The sendto() function's prototype is this:

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
               const struct sockaddr *dest_addr, socklen_t addrlen);

但我想发送一个结构作为第二个参数.我有可能这样做吗?还是 buf 必须是字符串?

But I would like to send a structure as the second argument. Is it possible for me to do that? Or does buf have to be a string?

你看,我必须能够发送这个:

You see, I have to be able to send this:

一条消息由HEADER"和PAYLOAD"组成.

"A message consists of "HEADER" followed by "PAYLOAD".

您的邮件标题应包含以下内容:

Your message header should consist of the following:

uint8_t version; /* must be 1. If you receive anything else, discard*/
uint8_t ttl; /* must be 1. If you receive anything else, discard*/
uint16_t payload_length; /* bytes following the header */
uint32_t account_identifier; /* digits of your account name */
uint32_t source_address; /* unused for now, set to 0 and ignore. */
uint32_t destination_address; /* unused for now, set to 0 and ignore. */
uint16_t checksum; /* unused for now, set to 0 and ignore. */
uint16_t protocol; /* must be 1. If you receive anything else, discard*/"

Payload 是一个文本字符串,比如Hello"之类的.

And payload is a text string like "Hello" or something.

我觉得我发送标头的唯一方法是发送所有信息的结构.对吗?

I feel like the only way for me to send the header would be to send a struct of all that info. Right?

推荐答案

您可以使用 sendto 发送任何连续的内存块,因此没有指针的 struct 将是美好的.但是,客户端需要能够使用正确的布局来处理该 struct,这是非常有限的.

You can send any contiguous block of memory with sendto, so a struct with no pointers would be fine. However, the client needs to be able to process that struct using the proper layout, which is extremely limiting.

考虑一个简单的 struct 像这样:

Consider a simple struct like this:

struct One {
    int a;
    float b;
    char c[20];
};

当客户端和服务器代码使用相同的编译器,使用相同的设置编译时,它在相同的硬件上运行良好.即使对于这个看似简单的struct,稍微不同的硬件也会导致问题,但是:字节序、填充、各种表示的大小等,为了使其正常工作,都将是相同的.

It works fine on the same hardware, when the client and the server code are compiled with the same compiler, using identical settings. A slightly different hardware will cause issues even for this seemingly simple struct, though: endianness, padding, the size of various representations, and so on, will all be the same in order for this to work.

如果您需要与其他硬件或其他编程语言的互操作性,您需要生成双方都可以处理的特定结构、二进制或文本.

If you need interoperability with other hardware or with other programming languages, you need to produce a specific structure, binary or a textual, that both sides can process.

如果你的 struct 有一个指针,如果没有一些特殊的处理,你将无法发送和接收它:

If your struct has a pointer, you would not be able to send and receive it without some special processing:

struct Two {
    int a;
    float b;
    char* c; // <<<=== Not going to work
};

当您需要发送这样的结构时,您可以在发送方将它们序列化"为可以由接收端反序列化"的字节数组,然后发送该缓冲区.接收这个缓冲区的代码将数据解包"到一个结构体"中,根据需要分配内存,并将接收方缓冲区中的数据复制到这个新分配的内存中.

When you need to send structures like that, you "serialize" them on the sender into an array of bytes that can be "deserialized" by the receiving end, and send that buffer instead. The code that receives this buffer "unpacks" the data into a `struct", allocates memory as needed, and copies the data from the receiver buffer into this newly allocated memory.

上面的 struct 可以序列化如下:

The struct above could be serialized as follows:

  • int
  • 的 4 个字节
  • float
  • 的 4 个字节
  • 4个字节表示指针c
  • 所指向的块的长度len
  • len 字节表示c
  • 的内容
  • 4 bytes for int
  • 4 bytes for float
  • 4 bytes representing the length len of the block pointed to by the pointer c
  • len bytes representing the content of c

这篇关于在sendto()函数中发送一个结构体——C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆