开发者

reading from stdin using select() - in a nonblocking way

i'm writing a program that perform server-client relations.

In the program, i'm using select() in order to get the client's r开发者_高级运维equests, and also the user's requests(the one behind the server) from the stdin.

What it seems to be is that the select() works fine for the client's requests, but doesn't seem to respond to the input from the stdin.

Also, i don't succeed to recv() from the stdin. Is there a way to get an input from the stdin in a non-blocking way? I've tried using fgets() instead of select(), and tho i've set the fcntl() to be non-blocking, it doesn't seem to work - it is still blocking.

What do you suggest? Thanks.


stdin is line-buffered - you cannot read from it in a non-blocking way.


You could always use kbhit to see if there are any characters on the input buffer ready to be read.

In the event you do not have access to kbhit, here is a simple implementation I have used in the past:

int kbhit() {
   int count = 0;
   struct termios otty, ntty;
   tcgetattr(STDIN_FILENO, &otty);
   ntty = otty;
   ntty.c_lflag &= ~ICANON;
   if(tcsetattr(STDIN_FILENO, TCSANOW, &ntty) == 0) {
      ioctl(STDIN_FILENO, FIONREAD, &count);
      tcsetattr(STDIN_FILENO, TCSANOW, &otty);
   }
   return count;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜