0

I got the following code:

TcpClient client = new TcpClient("server", 5555);

The server immediately sends back a ICMP port-unreachable but the program is taking about 30 seconds until it times out with a 10054 Error.

I turned the firewall off and wireshark is also capturing the packet, so it is not a windows-configuration problem.

How can i get the TcpClient to recognize the port-unreachable packet?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Zulakis
  • 7,859
  • 10
  • 42
  • 67

1 Answers1

0

An open ports immediately returns ICMP packet which is handled immediately by TcpClient however with a closed/blocked ports "Port Unreachable" response TcpClient has to wait till 30 seconds timeout. You can change the timeout method by making the Async call something as below:

TcpClient client = new TcpClient();
client.BeginConnect("server", 5555, new AsyncCallback(CallBack), client);

private void CallBack(IAsyncResult result)
{
 bool connected = false;
 using (TcpClient client = (TcpClient)result.AsyncState)
 {
    try
    {
        client.EndConnect(result);
        connected = client.Connected;
    }
    catch (SocketException)
    {
    }
 }
 if (connected)
 {
    this.Invoke((MethodInvoker)delegate
    {
        // Do Something
    });
 }
 else
 {
    this.Invoke((MethodInvoker)delegate
    {
        // Do Something
    });
 } 
}
AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65