为什么选择()不返回?

发布于 2025-02-10 17:22:10 字数 1008 浏览 2 评论 0原文

我正在尝试了解如何在处理服务器程序中的多个连接时使用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;
}

这是类服务器的函数之一,该功能处理连接,socsocket由函数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-02-17 17:22:10

根据 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>,writefdsexcepffds)。对于readfdsselect()如果以下任何条件适用于集合中的套接字:

  • if listic()已被调用在套接字上,连接正在等待,因此下一个accept()调用它不会阻止。
  • 数据可用于阅读。
  • 插座上公认的连接已关闭。

select()返回之后,readfdset将进行修改,并且仅包含符合上述条件的插座。在您的情况下,由于soc是集合中唯一的套接字,因此它将包含soc

如果您不希望select()可以阻止,则可以创建一个timeVal struct类似:

// timeout after 5s
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;

然后,您可以将其传递给select()

int res = select(FD_SETSIZE, &readFdSet, NULL,NULL, &timeout);

According to https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select, the select() function blocks if the timeout argument is NULL. So in the first iteration of the while loop, the program will block on the select() call until a socket condition is met, therefore not printing the second printf() statement.

The conditions which must be met for select() to continue execution differ depend on whether you pass readFdSet as the 2nd, 3rd or 4th argument (i.e. as readfds, writefds or exceptfds). For readfds, select() will continue if any of the following conditions apply for the socket in the set:

  • If listen() has been called on the socket, and a connection is pending, such that the next accept() call on it would not block.
  • Data is available for reading.
  • An accepted connection on the socket has been closed.

After select() returns, readFdSet will have been modified and contain only those sockets that meet the above conditions. In your case, since soc is the only socket in the set, it will contain soc.

If you do not want select() to block, you can create a timeval struct like so:

// timeout after 5s
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;

Then you can pass it to select():

int res = select(FD_SETSIZE, &readFdSet, NULL,NULL, &timeout);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文