C:关于 Beej 网络指南的问题...这里有一个假设吗?

问题描述 投票:0回答:3

我刚刚浏览了 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 并不总是读取套接字上要读取的“所有内容”,有时它只能返回其中的一部分。那么这样的话,这段代码岂不是错误的吗?我的意思是,在一次读取后,连接将被关闭......相反,我们不应该有其他一些机制吗?如果是这样,正确的方法是什么?

c sockets networking posix-select
3个回答
4
投票

只有当recv()发生错误时,套接字才会被关闭,否则它将处理已读取的数据,即使它没有全部读取。当它再次循环时,它会读出更多内容。确定这就是您要问的吗?


1
投票

是的,您会继续阅读,直到获得所需的所有数据,显然您需要以某种方式知道预期的数据 - 这就是为什么 http 将文档大小放在第一位


1
投票

当recv()返回负值时,您唯一调用close,这意味着recv有某种错误。请注意,您进行关闭的块有一条注释,指出

// got error or connection closed by client
)。

当你实际获取一些数据时(以

// we got some data from a client
开头的else分支),连接并没有被关闭。

你是对的,你不能假设数据一次性全部到达。您的错误在于遵循代码的工作原理。

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