c - select() 遇到问题

标签 c ansi posix-select

经过多次尝试后,我的 select() 无法正常工作。我好迷路!结果是 -1。我正在关注 this guide .

listen(sock, MAXQUEUE);
build_select_list(sock, connectlist, highsock, socks);

readsocks = select(FD_SETSIZE, &socks, (fd_set *) 0, (fd_set *) 0,
        NULL );

这些是我正在使用的函数:

void build_select_list(int sock, int connectlist[], int highsock, fd_set socks) {
int listnum;

FD_ZERO(&socks);

FD_SET(sock, &socks);

/* Loops through all the possible connections and adds
 those sockets to the fd_set */

for (listnum = 0; listnum < MAXQUEUE; listnum++) {
    if (connectlist[listnum] != 0) {
        FD_SET(connectlist[listnum], &socks);
        if (connectlist[listnum] > highsock)
            highsock = connectlist[listnum];
    }
}}

这就是我获取监听器文件描述符的方式:

int socketServer(char* portNumber) {

    int sockfd; // listen on sock_fd, new connection on new_fd
    struct addrinfo hints, *servinfo, *p;
    struct sigaction sa;
    int yes = 1;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP
    if ((rv = getaddrinfo(NULL, portNumber, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    // loop through all the results and bind to the first we can
    for (p = servinfo; p != NULL ; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol))
                == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))
                == -1) {
            perror("setsockopt");
            exit(1);
        }
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }
        break;
    }
    if (p == NULL ) {
        fprintf(stderr, "server: failed to bind\n");
        return 2;
    }
    freeaddrinfo(servinfo); // all done with this structure
    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }
    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL ) == -1) {
        perror("sigaction");
        exit(1);
    }
    return sockfd;

最佳答案

您的build_select_list 函数获取集合的副本,并且仅修改该本地副本。 “通过引用”传递它,就像您对 select 函数所做的那样。

当然,每次调用 select 之前,您都必须调用此函数,因为 select 函数会修改集合。

关于c - select() 遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17387936/

相关文章:

c - 具有不同优化级别的 GNU 并行运行 Makefile

date - 1601 年 1 月 1 日有什么意义?

python - 无法在 Mac OS 中使用 Python select.poll?

c - 如何使用c自动启动PC

c - 无法递归访问子文件夹

c - 如果我在 "sync"之前关闭设备会发生什么?

C 代码兼容性

emacs - 如何在任何模式下在 emacs 中显示 ANSI 颜色代码?

c - 将 select() 与管道一起使用