Setting timeout on socket connect
-
Does anyone know how I can set a timeout value for the connect() method of a socket? If it can't connect within about 200ms then I know that it never will. At the moment the connect is waiting for about 5-10 seconds before it returns. My code below:
target.sin_family = AF_INET; target.sin_addr.s_addr = *(long *)(host->h_addr); target.sin_port = htons(port); s=socket(AF_INET,SOCK_STREAM,0); if (s<0) exit(printf("Error creating socket.\n")); if (!connect(s,(struct sockaddr *)&target, sizeof(target))) { // ....... do my thing here .... } if (closesocket(s)) exit(printf("error closing socket.\n"));
-
Does anyone know how I can set a timeout value for the connect() method of a socket? If it can't connect within about 200ms then I know that it never will. At the moment the connect is waiting for about 5-10 seconds before it returns. My code below:
target.sin_family = AF_INET; target.sin_addr.s_addr = *(long *)(host->h_addr); target.sin_port = htons(port); s=socket(AF_INET,SOCK_STREAM,0); if (s<0) exit(printf("Error creating socket.\n")); if (!connect(s,(struct sockaddr *)&target, sizeof(target))) { // ....... do my thing here .... } if (closesocket(s)) exit(printf("error closing socket.\n"));