I'm creating a small tool which let me to check status of ports of given IP and port number like this:
private void ScanPort(IPAddress address, int port)
{
using (TcpClient client = new TcpClient())
{
try
{
client.Connect(address, port);
txtDisplay.AppendText("Port: " + nudFrom.Value.ToString() +
" is open." + Environment.NewLine);
}
catch (SocketException)
{
txtDisplay.AppendText("Port: " + nudFrom.Value.ToString() +
" is closed." + Environment.NewLine);
}
}
}
The problem is that when port is open it takes about a second to check and if port is closed the process takes about 20 seconds time.
How to make checking for open/closed ports quicker?