I am developing a RTSP Source filter in C++, and I am using WINSOCK 2.0 - blocking socket.
When I create a blocking socket, I set its SO_RCVTIMEO
to 3 secs like so:
int ReceiveTimeout = 3000;
int e = setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&ReceiveTimeout, sizeof(int));
My filter tries to connect to IP_ADDRESS:554
(554 is RTSP server port). If there is a server listening on that IP on the port 554, all goes well, but:
If my filter creates a socket to an existing IP address, but on a random port which no one listens on,
connect()
waits for 3 secs and returnsWSAETIMEDOUT
. So after 3 secs, I know that the provided URL is bad.If my filter creates a socket to a non existing IP address, and tries to connect it, it hangs for about 10 secs before returning SOCKET_ERROR. So,
SO_RCVTIMEO
gets ignored if the IP doesn't exist on the network...
QUESTION: How can I set the timeout for a non existing IP, in the second case? Do I need to send ICMP PING first to see does the IP exist, or perform some other check like that?
Any help will be appreciated. Thanx. :)
THE ANSWER TO MY PROBLEM
Because I am using blocking sockets, call to connect()
blocks, until the connection is made, or the connection fails because the host is not responding, or it is refusing connection. If I set socket's timeout to be 3 seconds, and try to connect to a host that doesn't exist, my pc (client) will send TCP packet with SYN
flag set, to initiate the Threeway handshake. Normally, the host, if up, will respond with TCP packet containing ACK
and SYN
flags set, and then, client (me) would send the TCP packet with ACK
flag set. Then the connection is made. BUT if the host is down, and the SYN
is sent, client waits until the 3 second timeout expires, and then tries AGAIN, and AGAIN, until the TcpMaxConnectRetransmissions
(MICROSOFT ARTICLE) registry setting is reached, because the host can be UP but the SYN
packet might get lost... My Windows XP has this setting at 4, I guess, so each time it tries to send SYN
, it waits 3 seconds, and when the fourth try fails, it returns SOCKET_ERROR
(after 12 secs), and sets WSAETIMEDOUT
as the last WSA error.
The way around this is using non blocking sockets, and trying to manually measure the connection attempt time (because now the connect()
wouldn't block) as Martin James suggested.
Another way is to fiddle with the registry, which is the last resort...