Is it possible to set a timeout when performing a port lookup as demonstrated in the code below?:
try
{
System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
sock.Connect(ipa, portno1);
if (sock.Connected == true) // Port is in use and connection is successful
{
displayGreen1();
}
sock.Close();
}
Looks like this may be what you’re looking for: http://www.codeproject.com/KB/IP/TimeOutSocket.aspx.
It seems that he’s using
to block the main thread for the duration of the time-out. If
is false (i.e., the connection was not made in time or the callBack failed) when time runs out, an exception will be thrown.