I'm looking for a piece of code that checks if a port if up. I know that there are other answered questions about this topic but they have a little issue. They are slow. If the port isn't open it freezes my app for about 5-6 seconds.
Code I found around ( and edited ):
public static bool IsServerOnline()
{
try
{
using (var client = new TcpClient())
{
var result = client.BeginConnect(SIMPLE_CONFIG.IP, SIMPLE_CONFIG.PORT, null, null);
// [declaration in SIMPLE_CONFIG.cs] SIMPLE_CONFIG.TIMEOUT = new Timespan(0,0,1); it should be 1 second
var success = result.AsyncWaitHandle.WaitOne(new TimeSpan(0,0,SIMPLE_CONFIG.TIMEOUT));
if (!success)
{
return false;
}
client.EndConnect(result);
}
}
catch
{
return false;
}
return true;
}
If the port is up it instantly returns true but if not it freezes the app for a few seconds even though i set a timeout of 1 second ( which doesn't seem to be working )