在 C 中通过套接字传输 int
的合适方法是什么?
到目前为止我正在做的是:
int n = 4;
int tmp = htonl(n);
write(socket, &tmp, sizeof(tmp));
和
int tmp,n;
read(socket, &tmp, sizeof(tmp));
n = ntohl(tmp);
但是,收到的整数有时为 0。并非总是如此,但假设 5 次中有 2 次。它永远不是其他值,始终为 0。为什么?
更新:读取的返回值为 -1,错误为:
Resource temporarily unavailable
最佳答案
首先,sizeof(int)
在您的发送方和接收方机器上可能不同。所以我建议您使用 stdint.h
中的 int32_t
之类的东西。
此外,不能保证 read(..,..,sizeof(int))
会准确读取 sizeof(int)
字节 - 它什么也读不到,或者它可以读取更少的字节。因此,正确的变体应该更像这样:
int send_int(int num, int fd)
{
int32_t conv = htonl(num);
char *data = (char*)&conv;
int left = sizeof(conv);
int rc;
do {
rc = write(fd, data, left);
if (rc < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
// use select() or epoll() to wait for the socket to be writable again
}
else if (errno != EINTR) {
return -1;
}
}
else {
data += rc;
left -= rc;
}
}
while (left > 0);
return 0;
}
int receive_int(int *num, int fd)
{
int32_t ret;
char *data = (char*)&ret;
int left = sizeof(ret);
int rc;
do {
rc = read(fd, data, left);
if (rc <= 0) { /* instead of ret */
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
// use select() or epoll() to wait for the socket to be readable again
}
else if (errno != EINTR) {
return -1;
}
}
else {
data += rc;
left -= rc;
}
}
while (left > 0);
*num = ntohl(ret);
return 0;
}
关于c - 通过 C 中的套接字传输整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140409/