为什么选择报告读取文件描述符已经准备好了?

时间:2015-07-05 16:51:43

标签: c linux select

为了测试select系统调用,我编写了一个程序来接收来自客户端的数据:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>

#define BACKLOG 5

int main(int argc, char *argv[])
{
    if (argc <= 2)
    {
        printf("Usage: %s ip_address port_number\r\n", basename(argv[0]));
        return 1;
    }
    const char *ip = argv[1];
    int port = atoi(argv[2]);

    int ret = 0;
    struct sockaddr_in address;
    memset(&address, 0, sizeof(address));
    address.sin_family = AF_INET;
    inet_pton(AF_INET, ip, &address.sin_addr);
    address.sin_port = htons(port);

    int listenfd = socket(AF_INET, SOCK_STREAM, 0);
    assert(listenfd != -1);
    ret = bind(listenfd, (struct sockaddr *)&address, sizeof(address));
    assert(ret != -1);
    ret = listen(listenfd, BACKLOG);
    assert(ret != -1);

    struct sockaddr_in client_address;
    socklen_t client_addrlen = sizeof(client_address);
    int connfd = accept(listenfd, (struct sockaddr *)&client_address, &client_addrlen);
    if (connfd < 0)
    {
        printf("accept failed! errno is %d\r\n", errno);
        close(listenfd);
    }
    else
    {
        printf("accept success!\r\n");
    }

    char buf[1024];
    fd_set read_fds;
    fd_set exception_fds;
    FD_ZERO(&read_fds); 
    FD_ZERO(&exception_fds);

    while (1)
    {
        memset(buf, 0, sizeof(buf));
        FD_ZERO(&read_fds);
        FD_ZERO(&exception_fds);
        FD_SET(connfd, &read_fds);
        FD_SET(connfd, &exception_fds);
        ret = select(connfd + 1, &read_fds, 0, &exception_fds, NULL);
        if (ret < 0)
        {
            printf("selection failed!\r\n");
            break;
        }
        if (FD_ISSET(connfd, &read_fds))
        {
            ret = recv(connfd, buf, sizeof(buf)-1, 0);
            if (ret < 0)
            {
                break;
            }
            printf("received %d bytes of normal data: %s\r\n", strlen(buf), buf);
        }
        else if (FD_ISSET(connfd, &exception_fds))
        {
            ret = recv(connfd, buf, sizeof(buf)-1, MSG_OOB);
            if (ret < 0)
            {
                break;
            }
            printf("received %d bytes of oob data: %s\r\n", strlen(buf), buf);
        }
    }

    close(connfd);
    close(listenfd);
    return 0; 
}

但是,当我从客户端程序发送数据时,上面的服务器程序打印如下: 接受成功!

received 16 bytes of normal data: thisisnormaldata
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data: 
received 0 bytes of normal data:

(无尽的&#34;接收o字节的正常数据&#34;)

为什么选择系统调用报告读取fds已经准备就绪?

1 个答案:

答案 0 :(得分:2)

select()报告您的FD始终准备就绪,因为 一直在准备好。它没有阻止(我推测)。

根据recv()文档的摘录,考虑不断打印的消息的含义:

  

成功完成后,recv()将以字节为单位返回消息的长度。如果没有可以接收的消息且对等体已经执行了有序关闭,则recv()将返回0.

由于您正在使用流套接字,因此它确实可以告诉另一端何时执行了有序关闭。你的程序告诉你这就是发生的事情。它会像你问的那样多次告诉你。对于您的程序而言,一个更典型的响应是重复再次询问,将关闭连接的本地端并继续处理其他事情(可能是处理其他连接)。

可能你想要select() listenfd文件描述符,以便等待后续的连接,虽然我看不出你会有什么好处,因为你在那时没有还有其他任何工作要做,只是执行阻止accept()会有问题。