为什么选择()不返回?
我正在尝试了解如何在处理服务器程序中的多个连接时使用select()
方法。
int Server::handler()
{
int iResult;
fd_set activeFdSet;
fd_set readFdSet;
FD_ZERO(&activeFdSet);
FD_SET(soc, &activeFdSet);
printf("FD_SETSIZE=%d\n", FD_SETSIZE);
while (1)
{
readFdSet = activeFdSet;
printf("\tCopied activefdset to readfdset\n");
int res = select(FD_SETSIZE, &readFdSet, NULL,NULL,NULL);
printf("Return value of select %d\n", res);
}
return 0;
}
这是类服务器
的函数之一,该功能处理连接,soc
是socket
由函数socket返回的
/代码>创建服务器套接字时。
在此功能中,似乎select()
根本没有返回任何内容,因为中的最后一个
printf()印刷到终端。
这就是我的程序输出的内容,然后即使连接到它也可以通过循环。
The winsock 2.2 dll was found
Set SO_KEEPALIVE: ON
FD_SETSIZE=64
Copied activefdset to readfdset
I am trying to understand how to use the select()
method when dealing with multiple connections in a server program.
int Server::handler()
{
int iResult;
fd_set activeFdSet;
fd_set readFdSet;
FD_ZERO(&activeFdSet);
FD_SET(soc, &activeFdSet);
printf("FD_SETSIZE=%d\n", FD_SETSIZE);
while (1)
{
readFdSet = activeFdSet;
printf("\tCopied activefdset to readfdset\n");
int res = select(FD_SETSIZE, &readFdSet, NULL,NULL,NULL);
printf("Return value of select %d\n", res);
}
return 0;
}
This is one of the functions in the class Server
that handles connection, soc
is the SOCKET
returned by the function socket()
when creating the server socket.
In this function, it seems that select()
does not return anything at all since the last printf()
in the while
loop does not get printed to the terminal.
This is what my program outputs, and then goes through a loop even when I connect to it.
The winsock 2.2 dll was found
Set SO_KEEPALIVE: ON
FD_SETSIZE=64
Copied activefdset to readfdset
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 https://learn.microsoft。 com/en-us/windows/win32/api/winsock2/nf-winsock2-select ,
select()
功能块如果time> timeout
参数为null 。因此,在的第一次迭代时,
循环时,该程序将在select()
调用呼叫上,直到满足插座条件,因此不打印第二个printf ()
语句。必须满足
select()
继续执行的条件不同,取决于您是否将readfdset
作为第二,第三或第4个参数(即read> readfds <) /code>,
writefds
或excepffds
)。对于readfds
,select()
如果以下任何条件适用于集合中的套接字:listic()
已被调用在套接字上,连接正在等待,因此下一个accept()
调用它不会阻止。select()
返回之后,readfdset
将进行修改,并且仅包含符合上述条件的插座。在您的情况下,由于soc
是集合中唯一的套接字,因此它将包含soc
。如果您不希望
select()
可以阻止,则可以创建一个timeVal
struct类似:然后,您可以将其传递给
select()
:According to https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select, the
select()
function blocks if thetimeout
argument is NULL. So in the first iteration of thewhile
loop, the program will block on theselect()
call until a socket condition is met, therefore not printing the secondprintf()
statement.The conditions which must be met for
select()
to continue execution differ depend on whether you passreadFdSet
as the 2nd, 3rd or 4th argument (i.e. asreadfds
,writefds
orexceptfds
). Forreadfds
,select()
will continue if any of the following conditions apply for the socket in the set:listen()
has been called on the socket, and a connection is pending, such that the nextaccept()
call on it would not block.After
select()
returns,readFdSet
will have been modified and contain only those sockets that meet the above conditions. In your case, sincesoc
is the only socket in the set, it will containsoc
.If you do not want
select()
to block, you can create atimeval
struct like so:Then you can pass it to
select()
: