1

I'm trying to create UDP port checker based on this question, however when testing (for example) www.google.com on port range 1-100, I receive for each port the exception "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond". Below is my code for testing the port:

try {
        UdpClient udpClient = new UdpClient(Port);
        Socket uSocket = udpClient.Client;
        uSocket.ReceiveTimeout = 5000;
        uSocket.Connect(Adress, Port);
        udpClient.Connect(Adress, Port);
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(Adress, Port);
        Byte[] sendBytes = Encoding.ASCII.GetBytes("?");
        udpClient.Send(sendBytes, sendBytes.Length);
        Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
} catch (SocketException e) {
        if (e.ErrorCode == 10054) {
             return false;
        } else {
             return false;
        }
}
return true;
Community
  • 1
  • 1
Michal Olechowski
  • 636
  • 1
  • 8
  • 25
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 27 '15 at 02:07

1 Answers1

1

UDP is a connectionless protocol. Since you didn't receive error 10054 (which is an explicit indication that the port is closed) but did get a TimeoutException, you should assume that the port is open, but the remote host just doesn't care to send any acknowledgement of your message. However, you'll also get a TimeoutException if a router or firewall is filtering that port. Since UDP is connectionless, there's just no way to know. Quick reference:

Got a reply? Port open.

Got a SocketException with error 10054? Port closed.

Got a TimeoutException? If there's a firewall or NAT router between you and the target, the port may be filtered (effectively closed). If you know for certain there's nothing filtering UDP traffic, the port is open.

Also see the Wikipedia page on UDP or connectionless communication.

Ben N
  • 2,883
  • 4
  • 26
  • 49
  • I never get any replay, based on this i should consider all tested ports are closed, however i know for a fact that for example port 80 is open but it still wont respond back to me. – Michal Olechowski Apr 27 '15 at 00:57
  • @user3290117 Web servers listen on **TCP** port 80. I don't think Google exposes any UDP services. TCP and UDP are entirely different protocols, and the same port number can be used by two services on the same machine if one uses TCP and the other uses UDP. – Ben N Apr 27 '15 at 00:59
  • Thank you for answer I thought it was UDP on port 80, this explains everything. – Michal Olechowski Apr 27 '15 at 01:02