TcpClient - waiting for connection
-
I am using TcpClient.Connect(ipaddress, portnumber) but the app must 'scan' a few possible IPs before finding the 'active' one. However if 'ipaddress' does not respond, the Connect() function hangs for nearly 60 seconds before raising an exception. Is there any way I can test for an IP quickly (e.g. a 'ping() command) before calling 'Connect()? I don't really want to get into starting separate threads for each possible IP address to search.
-
I am using TcpClient.Connect(ipaddress, portnumber) but the app must 'scan' a few possible IPs before finding the 'active' one. However if 'ipaddress' does not respond, the Connect() function hangs for nearly 60 seconds before raising an exception. Is there any way I can test for an IP quickly (e.g. a 'ping() command) before calling 'Connect()? I don't really want to get into starting separate threads for each possible IP address to search.
-
I don't have access to the documentation right now but I thought you could set a time out on the underlying socket via TcpClient? -Jeff
-
I am using TcpClient.Connect(ipaddress, portnumber) but the app must 'scan' a few possible IPs before finding the 'active' one. However if 'ipaddress' does not respond, the Connect() function hangs for nearly 60 seconds before raising an exception. Is there any way I can test for an IP quickly (e.g. a 'ping() command) before calling 'Connect()? I don't really want to get into starting separate threads for each possible IP address to search.
Try this code snippet.
try
{
System.Net.Dns.GetHostByAddress(this.serverAddress);
}
catch (System.Net.Sockets.SocketException)
{// IP-address isn't available.
}
-
Try this code snippet.
try
{
System.Net.Dns.GetHostByAddress(this.serverAddress);
}
catch (System.Net.Sockets.SocketException)
{// IP-address isn't available.
}
Stefan, Thanks but I think this will only be OK if there is a domain name server to provide a name. This system needs to work on LANs where there may not be one. I guess I'll need to use the asynch StartConnect() method but I need a decent sample to see how to use it.
-
Stefan, Thanks but I think this will only be OK if there is a domain name server to provide a name. This system needs to work on LANs where there may not be one. I guess I'll need to use the asynch StartConnect() method but I need a decent sample to see how to use it.
I don't think so. The MSDDN doesn't state that you need the presence of a real DNS in your Network and i successfully used this method in my local LAN to determine whether a PC can be reached or not.