在C使用与选择标准输入() [英] Using stdin with select() in C

查看:104
本文介绍了在C使用与选择标准输入()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序:

 #include <stdio.h>
 #define STDIN 0

 int main()
 {

    fd_set fds;
    int maxfd;
    // sd is a UDP socket

    maxfd = (sd > STDIN)?sd:STDIN;

    while(1){

        FD_ZERO(&fds);
        FD_SET(sd, &fds); 
        FD_SET(STDIN, &fds); 

        select(maxfd+1, &fds, NULL, NULL, NULL); 

        if (FD_ISSET(STDIN, &fds)){
              printf("\nUser input - stdin");
        }
        if (FD_ISSET(sd, &fds)){
              // socket code
        }
     }
 }

我面临的问题是,一旦对STDIN检测输入,将显示用户输入 - 标准输入不断印刷...为什么没有打印一次,并将在下次while循环检查其描述符的具有输入?

The problem I face is that once input is detected on STDIN, the message "User input - stdin" keeps on printing...why doesn't it print just once and on next while loop check which of the descriptors has input ?

感谢。

推荐答案

选择函数只告诉你什么时候有可用的输入。如果你不实际消费它,选择继续直通下降。

The select function only tells you when there is input available. If you don't actually consume it, select will continue falling straight through.

这篇关于在C使用与选择标准输入()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆