Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active August 29, 2015 14:07
Show Gist options
  • Save CatTail/ece5d66d510adf136500 to your computer and use it in GitHub Desktop.
Save CatTail/ece5d66d510adf136500 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int max(int a, int b) {
return a > b ? a : b;
}
int main()
{
int fd1, fd2;
fd_set readfs;
struct timeval tv;
int maxfd, result;
char buf[BUFSIZ];
fd1 = fileno(stdin);
fd2 = fileno(stdin); // socket(...);
maxfd = max (fd1, fd2) + 1;
tv.tv_sec = 5;
tv.tv_usec = 0;
/* loop for input */
while (1) {
FD_ZERO(&readfs);
FD_SET(fd1, &readfs);
FD_SET(fd2, &readfs);
/* block until input becomes available */
result = select(maxfd, &readfs, NULL, NULL, &tv);
switch(result)
{
case -1:
perror("select()");
break;
case 0:
printf("Timeout\n");
break;
default:
if (FD_ISSET(fd1, &readfs))
{
read(fd1, buf, sizeof(buf));
printf("%s", buf);
}
if (FD_ISSET(fd2, &readfs))
{
read(fd2, buf, sizeof(buf));
printf("%s", buf);
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment