Socket Programming: Connect with timeout
http://developerweb.net/forum/showthread.php?p=13486 關鍵是下列幾行: // Set non-blocking if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) { fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); exit(0); } arg = O_NONBLOCK; if( fcntl(soc, F_SETFL, arg) < 0) { fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); exit(0); }