#include #include #include #include #include #include #include #include #include static int AT = 5; /* usecs for _ualarm to */ int nonblocking(int fd){ int fb = fcntl(fd, F_GETFL, 0); if (fb == -1) return -1; return fcntl(fd, F_SETFL, fb|O_NONBLOCK); } void sigalrm(int arg){ ualarm(AT,0); } int try_connect(){ int s,c; struct sockaddr_in serv; memset(&serv,0,sizeof(serv)); s = socket(PF_INET,SOCK_STREAM,6); nonblocking(s); serv.sin_family = AF_INET; serv.sin_port = htons(80); inet_aton("127.0.0.1",(struct in_addr*)&serv.sin_addr); c = connect(s,(struct sockaddr*)&serv,sizeof(serv)); if( c < 0 && errno == EINTR ) perror("connect (EINTR):"); // this is return c; } int main( int argc, char** argv ){ signal(SIGALRM,sigalrm); ualarm(AT,0); while(1){ try_connect(); if( errno == EBADF ){ break; } sleep(100); // this sleep never really sleeps 100 secs. } puts("ran out of file descriptors as expected."); return 0; }