FD_ISSET 返回 0

发布于 2025-01-11 03:25:54 字数 1501 浏览 0 评论 0原文

我在这里看到过类似的帖子,但我仍然找不到答案。 我正在尝试在 Windows 上用 C 语言执行此接收函数:

void receiving(int server_fd){
struct sockaddr_in address;
int valread;
char buffer[2000] = {0};
int addrlen = sizeof(address);
fd_set current_sockets, ready_sockets;

//Initialize my current set
FD_ZERO(&current_sockets);
FD_SET(server_fd, &current_sockets);
int k = 0;
while (1)
{
    k++;
    ready_sockets = current_sockets;

    if (select(FD_SETSIZE, &ready_sockets, NULL, NULL, NULL) < 0)
    {
        perror("Error");
        exit(EXIT_FAILURE);
    }
    for (int i = 0; i < FD_SETSIZE; i++)
    {
        if (FD_ISSET(i, &ready_sockets))
        {

            if (i == server_fd)
            {
                int client_socket;

                if ((client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
                {
                    perror("accept");
                    exit(EXIT_FAILURE);
                }
                FD_SET(client_socket, &current_sockets);
            }
            else
            {
                valread = recv(i, buffer, sizeof(buffer), 0);
                printf("IP address recv2 is: %s\n", inet_ntoa(address.sin_addr));
                printf("\n%s\n", buffer);
                FD_CLR(i, &current_sockets);
            }
        }
    }

    if (k == (FD_SETSIZE * 2))
        break;
}

}

不幸的是,程序没有进入 if(FD_ISSET(i,&ready_socket))。 FD_ISSET 始终返回 0。 有谁知道为什么? 谢谢

I've seen a similar post here, but I still can't figure out the answer.
I’m trying to do this receiving function in C on Windows:

void receiving(int server_fd){
struct sockaddr_in address;
int valread;
char buffer[2000] = {0};
int addrlen = sizeof(address);
fd_set current_sockets, ready_sockets;

//Initialize my current set
FD_ZERO(¤t_sockets);
FD_SET(server_fd, ¤t_sockets);
int k = 0;
while (1)
{
    k++;
    ready_sockets = current_sockets;

    if (select(FD_SETSIZE, &ready_sockets, NULL, NULL, NULL) < 0)
    {
        perror("Error");
        exit(EXIT_FAILURE);
    }
    for (int i = 0; i < FD_SETSIZE; i++)
    {
        if (FD_ISSET(i, &ready_sockets))
        {

            if (i == server_fd)
            {
                int client_socket;

                if ((client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
                {
                    perror("accept");
                    exit(EXIT_FAILURE);
                }
                FD_SET(client_socket, ¤t_sockets);
            }
            else
            {
                valread = recv(i, buffer, sizeof(buffer), 0);
                printf("IP address recv2 is: %s\n", inet_ntoa(address.sin_addr));
                printf("\n%s\n", buffer);
                FD_CLR(i, ¤t_sockets);
            }
        }
    }

    if (k == (FD_SETSIZE * 2))
        break;
}

}

Unfortunately, the program doesn’t enter in the if(FD_ISSET(i,&ready_socket)). FD_ISSET always returns 0.
Does anyone know why ?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文