gpt4 book ai didi

c - C 套接字中的非阻塞 recv()

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

我在套接字中使用无限循环,如果它收到一些数据,它应该接收它,或者如果它想发送它发送的数据。像下面给出的东西。我正在使用选择。我只有一个socket sd。

fd_set readsd;
int maxsd = readsd +1;
// all those actions of setting maxsd to the maximum fd +1 and FDSETing the FDs.
while(1)
{
FD_ZERO(&read_sd);
FD_SET(sd, &read_sd);
if(FD_ISSET(sd, &readsd))
{
//recv call
}
else
{
//send call
}
}

据我所知,select 选择数据最先到达的套接字描述符之一。但是这里我只有一个套接字,如果有数据我想接收,否则我想发送。

在那种情况下,上面给出的代码可以吗?或者还有其他我不知道的选择?

最佳答案

In that case, is the code given above fine ?

我没有看到任何要选择的电话。此外,如果“maxsd”被设计为 select 的第一个参数,它的值是错误的:它必须是最大的文件描述符 +1。无论如何,您可以简单地使用标志 MSG_DONTWAIT 调用 recv,在这种情况下,如果没有数据可读,它将返回错误。

关于c - C 套接字中的非阻塞 recv(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16564821/

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