linux select详解

select原理

Linux 系统编程——与内核和 C 库直接对话
select能轮询一个管道端口(文件,网络),若有数据

select例子

#include 
#include 
#include 
#include 

const int TIMEOUT = 5;
const int BUF_LEN = 1024;

int main() {
    struct timeval tv;
    fd_set readfds;
    int ret;

    FD_ZERO(&readfds);
    FD_SET(STDIN_FILENO, &readfds);

    tv.tv_sec = TIMEOUT;
    tv.tv_usec = 0;

    ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
    if (ret == -1) {
        perror("select");
        return 1;
    } else if (!ret) {
        printf("%d seconds elapsed.\n", TIMEOUT);
        return 0;
    }

    if (FD_ISSET(STDIN_FILENO, &readfds)) {
        char buf[BUF_LEN + 1];
        int len;
        len = read(STDIN_FILENO, buf, BUF_LEN);
        if (len == -1) {
            perror("read");
            return 1;
        }
        if (len) {
            buf[len] = '\0';
            printf("read:%s\n", buf);
        }
        return 0;
    }
    fprintf(stderr, "This should not happend!\n");
    return 1;
}

你可能感兴趣的:(C++网络编程)