Well it goes something like this
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <termios.h>
void set_nonblock ( ) {
struct termios newt;
tcgetattr( 0, &newt );
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( 0, TCSANOW, &newt );
}
int main ( ) {
int status;
set_nonblock();
while ( 1 ) {
int max_fd = 0; /* max fd to be watched - currently only stdin */
fd_set read_set; /* file descriptors to watch */
struct timeval tmo; /* timeout */
FD_ZERO(&read_set);
FD_SET(0,&read_set); /* watch for changes on stdin */
tmo.tv_sec = 1;
tmo.tv_usec= 0; /* wait for up to 1 second */
status = select( max_fd+1, &read_set, 0, 0, &tmo );
switch ( status ) {
case -1:
/* an error */
perror( "select" );
break;
case 0:
/* timeout */
printf( "select timed out\n" );
break;
default:
/* something worth doing - check each fd in each set */
if ( FD_ISSET(0,&read_set) ) {
char ch;
int n = read( 0, &ch, sizeof(ch) );
printf( "Read char %c(%d)\n", ch, n );
}
break;
}
}
return 0;
}
Add your socket fd's to the read_set and bump max_fd appropriately and add another FD_ISSET() test.