c - 通过套接字c读取整数

标签 c int

我需要通过套接字读取一个整数并将其感知到一个函数。我愿意

strcpy(out,"insert id messagge\n");
if (write(sd_client,out, sizeof(out))==-1){
    printf("Error.\n");
    }
while ((read(sd_client, &id, sizeof(int)))==-1){  //id is an integer
        if(errno!=EINTR){
    printf("Error.\n");
        exit(-1);
    }
}
messaggio2(sd_client, logi, atoi(id)); //atoi(id) try to send int to func

有人可以帮助我吗? :D

最佳答案

readwrite 的第二个参数是指向数据的指针

当你说:

write(sd_client,out, sizeof(out))

您正在传递 out 的。应该是:

write(sd_client, &out, sizeof(out))

此外,我认为您已将 id 声明为 int (这是正确的),那么为什么要将它传递给 atoi ?该函数用于从字符串中解析 int。

关于c - 通过套接字c读取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801526/

相关文章:

C UINT16 如何正确?

python - 在逻辑中使用 numpy 形状输出

命令 int 无法计算大数?

c - 如何将字符串作为参数传递给 C 宏

c - 在数组中存储结构体时遇到问题

c - c 中的 TCP 聊天无法正确读取/发送字符串

C 中不区分大小写的字符串比较

c++ - 为什么在 C/C++ 的这个 while 循环中解引用只发生一次?

我可以在 C 中将 0.16、0.32 等正确转换为整数(16、32 等)吗?

在c中无法将int初始化为0