为什么select报告读文件描述符不断准备好?

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

为了测试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:

(无尽的“收到o字节的正常数据”)

为什么select系统调用不断报告读取的fds已准备好?

c linux posix-select
1个回答
2
投票

select()
报告您的 FD 始终准备就绪,因为它is 始终准备就绪。它不会阻塞(我猜)。

根据

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

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

由于您使用的是流套接字,因此它确实可以判断另一端何时执行了有序关闭。你的程序告诉你这就是发生的事情。你问多少次,它就愿意告诉你多少次。对于您的程序来说,比再次重复询问更典型的响应是关闭连接的本地端并继续执行其他操作(可能例如处理其他连接)。

可能您想要

select()
listenfd
文件描述符来等待后续连接,尽管我不认为这对您有什么好处,因为您此时没有任何其他工作要做所以仅仅执行阻塞
accept()
就会有问题。

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