-1

TcpClient of .Net allows me to test if a connection can be stablished, so, let me know if the remote port is Open.

Check this code:

TcpClient tcpCLient = new TcpClient();
        try
        {
            tcpCLient.Connect("www.google.com", 80);
            Console.WriteLine("Yes");
            Console.ReadLine();

        }
        catch (Exception)
        {

            Console.WriteLine("No");
            Console.ReadLine();
            throw;
        }

I tested the Tcp port 80 of www.google.com and because it is open I see in the console a "Yes" message. Now, I'll try the TCP port 70 of www.google.com that is closed.

 TcpClient tcpCLient = new TcpClient();
            try
            {
                tcpCLient.Connect("www.google.com", 70);
                Console.WriteLine("Yes");
                Console.ReadLine();

            }
            catch (Exception)
            {

                Console.WriteLine("No");
                Console.ReadLine();
                throw;
            }

I expected a exception, so I shoul see a "No" in the Console. Why Nothing happens? Thank you!!

Update: The console keeps this way even after several minutes. enter image description here

Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
  • can you expand on 'nothing happens'. Did you get either message. How long did you wait, there may be timeout and retry issues – pm100 Jan 08 '13 at 01:39
  • 1
    i see the same thing - u have to wait a few minutes. The reason is almost certainly due to google firewall, they drop packets for unexpected ports - this looks like a tranmission error so you get retries. Try the same test to a non firewalled machine (localhost for example) and it will fail instantly becuase host reject the request to closed port – pm100 Jan 08 '13 at 01:45
  • @pm100 You are right. So, what is the best way for test an open port if the firewalled machines make you wait lot of time? – Ricardo Polo Jaramillo Jan 08 '13 at 01:52
  • reduce the timeout on the tcpclient – pm100 Jan 08 '13 at 02:03
  • how? the properties SendTimeout and ReceiveTimeout doesnt see to work. – Ricardo Polo Jaramillo Jan 08 '13 at 02:12

2 Answers2

0

In short: Why you throw exception once you caught it?

This might be the cause of hanging console app, as it is waiting for timeout. In addition, as mentioned, it might be a firewall in remote address, as it may drop your calls. Let me know, if you get some other specific exceptions.

In addition, hopefully your code is handling the socket exceptions, as it is common exception while client tries to connect. Here is a related post - C# SocketException doesn't get caught

Community
  • 1
  • 1
Yusubov
  • 5,815
  • 9
  • 32
  • 69
0

@pm100 give me the answer in a commment.

"The reason is almost certainly due to google firewall, they drop packets for unexpected ports - this looks like a tranmission error so you get retries. Try the same test to a non firewalled machine (localhost for example) and it will fail instantly becuase host reject the request to closed port "

After some minutes You see the "No".

Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83