gpt4 book ai didi

c - 在 c 中选择显示奇怪的行为

转载 作者:太空宇宙 更新时间:2023-11-04 08:55:43 26 4
gpt4 key购买 nike

我写了一些代码,它应该遍历套接字列表,向它们发送数据包,如果得到答复,则将其发送回特定套接字。

这是我的代码片段:

 while ((curr_fd = conn_get_node()) > 0) {

send(curr_fd, fifo_packet.packet, MSGLENGTH, 0);

FD_ZERO(&rfds);
FD_SET(curr_fd, &rfds);

got_answer = select(curr_fd + 1, &rfds, NULL, NULL,
&tv);

if (got_answer == -1) {
perror("select()\n");
} else if (got_answer == 1) {
get_packet(curr_fd, &answer);
send(fifo_packet.from_fd, &answer, MSGLENGTH, 0);


} else {
printf("no data within 100us\n");
continue;
}

现在它显示对于发送的前两个或三个数据包,select 总是说它没有收到任何东西(got_answer = 0),尽管我可以看到数据已经通过嗅探器到达。大约第三个数据包后,它开始工作 (got_answer = 1)。

有人知道我做错了什么吗?

感谢您的帮助。

最佳答案

您没有检查 select() 何时返回 fd 是否仍在“ready for read”集合中。使用 FD_ISSET() 来检查这一点。这是必需的,因为 select() 可以因数据可用以外的其他原因返回。

关于c - 在 c 中选择显示奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17209991/

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