标题:初步建立一下对 select() 函数的印象_2
只看楼主
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
 问题点数:0 回复次数:0 
初步建立一下对 select() 函数的印象_2
自己动手,编译运行会发现3秒内不给出数据,select()就会反复不停的执行

然后打印 No data within 3 seconds, 有时候几行简单的实例比文字说明更直观了

程序代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>

int main(void) {

    int keyboard;

    int ret, i;

    char c;

    fd_set readfd;

    struct timeval timeout;

    keyboard = open( "/dev/tty", O_RDONLY | O_NONBLOCK);

    assert(keyboard > 0);

    while(1) {

        timeout.tv_sec = 3;

        timeout.tv_usec = 0;

        FD_ZERO(&readfd);

        FD_SET (keyboard, &readfd);

        ret = select ( keyboard + 1, &readfd, NULL, NULL, &timeout);

        if(0 == ret ) 

            printf("No data within 3 seconds\n");

        if( FD_ISSET ( keyboard, &readfd)) {

            i = read(keyboard, &c, 1);

            if( '\n' == c )

                continue;

            printf("wzj typed '%c'\n", c);

            if('q' == c )

                break;
        }
    }
}
搜索更多相关主题的帖子: seconds include keyboard 印象 
2012-08-30 08:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-380330-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.508902 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved