gpt4 book ai didi

c - TCP 套接字 C 上的负数

转载 作者:可可西里 更新时间:2023-11-01 02:44:58 24 4
gpt4 key购买 nike

每当我尝试通过 TCP 套接字发送一个负数时,当我打印收到的内容时,它会显示为“4.29497e+09”。我所做的就是:

int i = -8;
int temp = htonl(i);
write(sock,&temp,4);

在服务器上:

int temp;
read(sock, &temp,4);
int read = ntohl(temp);
cout << read << endl;

如果有人能提供帮助,我们将不胜感激。

最佳答案

htonl/ntohl 函数专门用于无符号 32 位整数。

The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.

关于c - TCP 套接字 C 上的负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14005693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com