I have an appication that needs to open a tcp socket
the problem is that if the address provided is not reachable
the open function wait for too long before returning the error code.
to speed up the process I usually set the the socket to be non blocking and use select with a timeout.
But it seems to be malfunctioning and the socket still wait an eternity to return with the error
Code:
int tcp_socket = 0;
tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
// set socket non-blocking
fcntl(tcp_socket, F_SETFL, fcntl(tcp_socket, F_GETFL, 0) | O_NONBLOCK);
// Initialize the file descriptor set
FD_ZERO(&fds);
// Add socket to set
FD_SET(tcp_socket, &fds);
// Connect
connect(................);
// Initialize the connection timeout structure
connectionTimeout.tv_sec = 0;
connectionTimeout.tv_usec = 200000;
// select blocks forever until data ready.
// select returns 0 if timeout, > 1 if input available, -1 if error.
n = select(tcp_socket+1, NULL, &fds, NULL, &connectionTimeout);
if(n < 0){
//ERROR
}else if(n == 0){
//TIMEOUT
}
Some idea on how I can get the desired behavior
Thanks