pmi*_*hna 1 c unix posix udp int64
我想int64_t通过UDP 发送两个.为此,我将它们存储在一个四元素数组中,其中:
int64_tint64_tint64_tint64_t我的发送代码:
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 */
Run Code Online (Sandbox Code Playgroud)
我通过UDP 接收后组合int32_t回来的代码:int64_tdata
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);
Run Code Online (Sandbox Code Playgroud)
第一个数字(from)始终是正确的.但是,我从第二个得到的printf是不正确的.更重要的是,它似乎取决于第一个数字.例:
发送:
from = 125,to = 20.收稿日期:
from = 125,to = 125.发送:
from = 1252,to = 20.收稿日期:
from = 1252,to = 1268.我究竟做错了什么?这是转换或通过网络发送的问题吗?
您的接收器代码中有一个拼写错误:
to = from | (int64_t) ntohl(data[2]);
Run Code Online (Sandbox Code Playgroud)
应该
to = to | (int64_t) ntohl(data[2]);
Run Code Online (Sandbox Code Playgroud)