本文介绍了字节顺序和Socket的C程序设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与使用C插座某些病人监护仪沟通的程序。我使用无连接插座(UDP)与所述设备进行通信。但是,我的计算机和设备,到目前为止,我这样做是为了获得病人监护仪解析响应之间的字节顺序不匹配:

I'm making a program that communicate with certain patient monitor using C sockets. I'm using connection-less sockets (UDP) to communicate with the device. But there is endianness mis-match between my computer and device and so far I was doing this to get parse response from the patient monitor:

recvfrom(int socket, char *buffer, size_t length, int flags,
             struct sockaddr *address, socklen_t *address_len);

然后我被铸造缓冲区直接的结构,用ntohs和和ntohl改变字节顺序,例如:

Then I was casting the buffer directly to structure and using the ntohs and ntohl to change the byte ordering, for example:

struct A * a = (struct A *)buffer;
Struct A b;
b.v1 = ntohs(a->v1);
b.v2 = ntohl(a->v2);

在网上阅读几个例子之后,我想通了,这可能是错误的做法,因为编译器相关的填充。但我不知道这件事。我需要简单的方法来取消马歇尔缓冲区C结构具有正确的字节序。那我收到的结构可以取消predictable长度和复杂一些的为好。我需要快速且简单的方法做未编组。

After reading few examples over internet, I figured out that this may be wrong approach due to compiler dependent padding. But I'm not sure about it. I need simple way to un-marshall a buffer to a C structure with correct endiannes. The structure that I'm receiving can be of unpredictable length and little complex as well. I need fast and easy approach to do the un-marshalling.

我不控制发件人。发件人在网络字节顺序。我的问题仅仅是: -
是否是安全蒙上了缓冲结构,然后用ntohs和和ntohl在此铸造结构,使得这个结构的主机字节顺序翻版?它是一个最快的方法?如果没有,那么什么可以最快的方法?

I don't control sender. Sender is in network byte order. My question is only that:-Is is safe to cast a buffer to a structure and then use ntohs and ntohl on this casted structure to make host-byte order replica of this structure? Is it a fastest approach? If not, then what can be the fastest approach?

推荐答案

首先,因为 A 是apointer,你的code应该以最低的做到这一点。 ..

First of all, since a is apointer, your code should at minimum do this...

b.v1 = ntohs(a->v1);
b.v2 = ntohl(a->v2);

所有的二,答案取决于情况和病人监护仪的规范。谁写code为病人监护仪?是否有它的规范?什么机器架构是使用(你知道的情况下),你处理的只是一个模型,还是有显示器的许多版本 - 你可以改变显示器等等等等

Second of all, the answer depends on circumstances and specification of the patient monitor. Who writes the code for the patient monitor? Is there a specification for it? What machine architecture is it using(in case you know), are you dealing with just one model or are there many versions of the monitor -- can you change the monitor, etc. etc.

我要去假设你的无法的改变显示器,而字节顺序在某处记录 - 你的可能的必须创建自己的解包/包例程,按字节寻址和位操作这样做 - 那就是,除非你知道的格式完全一致,即网络命令 - 和结构的填充是在网络缓冲区相同

Im going to assume that you cannot change the monitor, and that the byte order is documented somewhere -- and you may have to create your own unpack/pack routines, by doing byte addressing and bit manipulation -- that is unless you know that the format exactly matches that of "network" order -- and that padding of structs are the same in the network buffer.

因此​​,像;

void unpack(struct *b, unsigned char *buffer)
{
   b->v1 = (buffer[0] <<8)|(buffer[1]);   
   b->v2 = (buffer[2] <<24)|(buffer[3] <<16)|(buffer[4] <<8)|(buffer[5]);
   etc....
}   

或者类似这样的,如果你preFER你ntohX;

or like this if you prefer to you ntohX;

void unpack(struct *b, unsigned char *buffer)
{
   b->v1 = ntohs(buffer+0);   
   b->v2 = ntohl(buffer+2);
   etc....
}   

然而,如果你的的控制监视器code,然后使用类似的将摆脱做位操作的所有复杂性和担心字节顺序....

However if you do control the monitor code, then using a tool like protocol buffers would get rid of all the complexity of doing bit manipulation and worry about byte orders....

这篇关于字节顺序和Socket的C程序设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:00