• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quickly detecting an unavailable address/port  RSS feed

 
Brian Manahan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code that connects to an address and port, and detects an unavailable address/port if an exception is thrown by the Socket constructor. Like so:



This works, but it takes between .5 and 1 seconds if the address/port is unavailable. I've tried using InetAddress.isReachable() to detect unavailability before creating the socket, but that did not work. Does anyone know a way to detect an unavailable address and port quickly?

Right now, everything's happening on localhost so the address is always available, but only a few ports are open.

Thanks
[ March 29, 2007: Message edited by: Brian Manahan ]
 
Brian Manahan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out it's a problem specific to Windows, trying to make the connection 5 times. I guess Linux detects it almost instantly. If you adjust HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxConnectRetransmissions then this can avoided, but an easier way is to create the socket with the empty constructor and then connect like so:

socket.connect(inetSocketAddress, timeout);

where timeout is in milliseconds.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!