我刚刚浏览了Beej的《网络指南》,并对这部分代码(特别标记为“从这里”和“到这里”)感到好奇:

// main loop
    for(;;) {
        read_fds = master; // copy it
        if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
            perror("select");
            exit(4);
        }

        // run through the existing connections looking for data to read
        for(i = 0; i <= fdmax; i++) {
            if (FD_ISSET(i, &read_fds)) { // we got one!!
                if (i == listener) {
                    // handle new connections
                    addrlen = sizeof remoteaddr;
                    newfd = accept(listener,
                        (struct sockaddr *)&remoteaddr,
                        &addrlen);

                    if (newfd == -1) {
                        perror("accept");
                    } else {
                        FD_SET(newfd, &master); // add to master set
                        if (newfd > fdmax) {    // keep track of the max
                            fdmax = newfd;
                        }
                        printf("selectserver: new connection from %s on "
                            "socket %d\n",
                            inet_ntop(remoteaddr.ss_family,
                                get_in_addr((struct sockaddr*)&remoteaddr),
                                remoteIP, INET6_ADDRSTRLEN),
                            newfd);
                    }
                } else {
                    // handle data from a client
                    //----------------- FROM HERE --------------------------
                    if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
                        // got error or connection closed by client
                        if (nbytes == 0) {
                            // connection closed
                            printf("selectserver: socket %d hung up\n", i);
                        } else {
                            perror("recv");
                        }
                        close(i); // bye!
                        FD_CLR(i, &master); // remove from master set
                    //----------------- TO HERE ----------------------------
                    } else {
                        // we got some data from a client
                        for(j = 0; j <= fdmax; j++) {
                            // send to everyone!
                            if (FD_ISSET(j, &master)) {
                                // except the listener and ourselves
                                if (j != listener && j != i) {
                                    if (send(j, buf, nbytes, 0) == -1) {
                                        perror("send");
                                    }
                                }
                            }
                        }
                    }
                } // END handle data from client
            } // END got new incoming connection
        } // END looping through file descriptors
    } // END for(;;)--and you thought it would never end!

    return 0;

现在,我知道read并不总是读取将在套接字上读取的“所有内容”,并且有时它只能返回其中的一部分。在那种情况下,此代码会不正确吗?我的意思是,读完一遍,连接就被关闭了……相反,我们不应该采用其他某种机制吗?如果是这样,这里正确的方法是什么?

最佳答案

仅当recv()出错时,套接字才会在那里关闭,否则,即使未全部读取,套接字也将处理已读取的数据。然后,当它再次循环通过时,它将读出更多信息。可以确定这是您要问的吗?

关于c - C:关于Beej的网络指南的问题……这里有一个假设吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2181339/

10-10 08:28