select() 仅适用于第一个循环

问题描述 投票:0回答:2
void async_socket_rcv(int clientfd);

int main(int argc, char *argv[])
{
    //Configure buffer to output immediately
    setvbuf(stdout, 0, _IONBF, 0);

    //Mysql version
    printf("MySQL client version: %s\n", mysql_get_client_info());

    //Initialize the FDs for the sockets
    int socketfd, clientfd;

    //Initialize the list of FDs to monitor for input
    fd_set fd_monitor;
    int select_res;
    FD_ZERO(&fd_monitor);

    struct timeval select_timeout;
    select_timeout.tv_sec = 3;
    select_timeout.tv_usec = 0;


    //Initialize the thread id
    pthread_t thread;

    ...

    //Create the listen socket
    socketfd = create_inet_server_socket("0.0.0.0", "5671", LIBSOCKET_TCP, LIBSOCKET_IPv4, 0);

    if(socketfd < 0){
        printf("Error creating listen socket!");
        return -1;
    }else{
        printf("Listening...");
    }

    FD_SET(socketfd, &fd_monitor);
    //FD_SET(STDIN_FILENO, &fd_monitor);

    while(1)
    {
        select_res = select(sizeof(fd_monitor) * 8, &fd_monitor, NULL, NULL, &select_timeout);

        if(FD_ISSET(socketfd, &fd_monitor))
        {
            clientfd = accept_inet_stream_socket(socketfd,0,0,0,0,0,0);
            pthread_create(&thread, NULL, async_socket_rcv, clientfd);
        }
        select_timeout.tv_sec = 3;
        select_timeout.tv_usec = 0;
    }

    return 0;

}

我正在尝试使用

select()
函数在客户端连接时调用函数,而不必阻止
accept()
函数。我需要能够在不中断服务器的情况下接受
stdin

我上面的代码可以运行,但只能运行一次。当我单步执行代码时,如果我在

select_res
first 迭代中连接到服务器,我可以看到 select() 为 1
only
。之后,什么也没有发生。

我做错了什么?

c multithreading sockets tcp posix-select
2个回答
4
投票

select
更改传入的
fd_set
,设置具有活动的描述符并取消设置那些没有活动的描述符。由于您没有在循环中重置
fd_monitor
,因此您使用了更改后的
fd_set

FD_ZERO
FD_SET
调用移至循环开头。


0
投票

虽然上面的答案 100% 正确,但它实际上说明了为什么现在使用 select() 听起来像 18 世纪。有一个很棒的 poll(),使用它可以让你摆脱所有的麻烦!

© www.soinside.com 2019 - 2024. All rights reserved.