【发布时间】:2013-06-11 10:42:50
【问题描述】:
我想通过 UDP 发送两个 int64_t。为此,我将它们存储在一个四元素数组中,其中:
- [0] - 第一个
int64_t的低 32 个 - [1] - 第一个
int64_t的高 32 位 - [2] - 第二个低 32 位
int64_t - [3] - 如果是第二个
int64_t,则高 32 位
我的发送代码:
int64_t from, to;
/* some logic here */
data[0] = htonl((int32_t) from);
data[1] = htonl((int32_t) (from >> 32));
data[2] = htonl((int32_t) to);
data[3] = htonl((int32_t) (to >> 32));
/* sending via UDP here */
我在通过 UDP 接收到data 后将int32_t 组合回int64_t 的代码:
int64_t from, to;
from = (int64_t) ntohl(data[1]);
from = (from << 32);
from = from | (int64_t) ntohl(data[0]);
to = (int64_t) ntohl(data[3]);
to = (to << 32);
to = from | (int64_t) ntohl(data[2]);
printf("received from = %" PRId64 "\n", from);
printf("received to = %" PRId64 "\n", to);
第一个数字 (from) 始终正确。但是,我从第二个printf 得到的信息是不正确的。更重要的是,它似乎依赖于第一个数字。示例:
发送:
-
from= 125, -
to= 20。
收到:
-
from= 125, -
to= 125。
发送:
-
from= 1252, -
to= 20。
收到:
-
from= 1252, -
to= 1268。
我做错了什么?是转换的问题还是网络发送的问题?
【问题讨论】:
-
我不确定,但是用有符号的数字而不是无符号的数字来做这种事情可以吗?例如。
(int32_t) from是签名溢出,对吧?这不是未指明的行为吗? en.wikipedia.org/wiki/Integer_overflow "在 C 编程语言中,有符号整数溢出导致未定义行为"