UDPClient Slow Connection

Hello,

I was developing an UDP sender service (for an applicatoin in my company) and I used the UdpClient, which came to be really handy and useful. But when I tried to send UDP datagrams to an unknown IP, this means a fake low IP (for example 10.198.0.105 whick was not present on my network) the UdpClient in fact sent the data but after a huge amount of time... 10 seconds or even more.

First I used the Udpclient Send method passing the byte array, the lenght, the string host and int port for the destination host. That method was producing the delay, so I started to split it a bit more making the Udpclient call for the "Connect" method and leaving the Send method only with the data, and therefore I saw that the delay came in the connect method and send was nearly instantaneous. So I decided to construct instead of the string host, an IPAdress with the class Dns.Resolve, and again, the delay switched to that instruction.

So the solution we found was creating the IP from a long type using the forming 4 byte array. And using the constructor of the IPAdress passing the converted long, no more delays were detected and the package was sent correctly to that IP.

I'm here to warn you in using the Udpclient and relying in Dns.Resolve that surely have something wrong in its own code.



Answer this question

UDPClient Slow Connection

  • RobGMiller

    I would love to see a network sniff when the delay is happening. Can you use either netmon or Ethereal to sniff the network and then send me the capture by e-mail My guess is that the OS level DNS APIs are waiting for a response on the DNS lookup. If you need instructions on using netmon, see http://blogs.msdn.com/dgorti/archive/2005/10/29/486887.aspx. If you don't have access to netmon, try using Ethereal.

  • epilotusa

    Thanks for posting this. I would like to see your code snippet and
    find out where you are getting the delay. Please post this sample
    and we will take a look

  • Shem1

    Ok, my fault to go for the shifting etc.

    IPAddress ipad = IPAddress.Parse("10.148.203.86");

    with this line provided by JonCole there's also no delay on getting the correct IPAddress but the issue of the delay was not solved yet. Why the other attemps waste such a huge amount of time getting no Exceptions and finally sending the data




  • peteb1959

    Oh I forgot to post that there is another constructor for the IPAddress Class consisting in a byte[]. We didn't know how to use it, because the Exception 'System.ArgumentException' was thrown all the time, we did this:
    UdpClient udp = new UdpClient(62280);
    string[] splitted = new String("10.198.209.86").Split('.');
    byte[] asbyte = new byte[4];
    asbyte[0] = byte.Parse( splitted[3] );
    asbyte[1] = byte.Parse( splitted[2] );
    asbyte[2] = byte.Parse( splitted[1] );
    asbyte[3] = byte.Parse( splitted[0] );
    //this line produces the Exception:
    IPAddress ipad = new IPAddress(asbyte);

    But the important issue is with the delay... not with the ways of constructing an IPAddress.

    Thanks in advance for your replies :)



  • rodri_ogri

    The problem seems to be related with the Dns.Resolve method that somewhere must be called inside the UdpClient.Send(byte[],int lenght,string host,int port) to from the right IPAdress from the string host.

    Caution: when the IP is inside the network, it works perfectly, it only delays about 10seconds (without any Exception that's the strange thing) on sending when the IP is outside or not present we tried with IP "10.148.203.86" that was not on our network but in a machine outside.

    Here I post the Code snipplets and where the delay was:

    --------1st attempt --------------
    private void Send(byte[] message_to_Send){
    Udpclient udp = new Udpclient(62280);
    //delay of 15s (more or less) came executing the following line
    udp.Send(message_to_Send, message_to_Send.Length,"10.148.203.86",49922);
    udp.Close()
    }
    ---------2nd attempt-----------------
    private void Send(byte[] message_to_Send){
    Udpclient udp = new Udpclient(62280);
    //delay of 15s (more or less) came executing the following line
    udp.Connect("10.148.203.86",49922);
    //this line below now is quick as a bat out of hell
    udp.Send(message_to_Send, message_to_Send.Length);
    udp.Close();
    }
    ---------3rd attempt--------------------
    private void Send(byte[] message_to_Send){
    Udpclient udp = new Udpclient(62280);
    //delay of 15s (more or less) came executing the following line
    IPAddress ipad = Dns.Resolve("10.148.203.86").AddressList[0];
    IPEndPoint ipe = new IPEndPoint(ipad,49922);
    udp.Connect(ipe);
    udp.Send(message_to_Send, message_to_Send.Length);
    udp.Close();
    }
    ------- finally the solution that has no delays was ---------
    UdpClient udp = new UdpClient(62280);
    string[] splitted = new String("10.148.203.86").Split('.');
    byte[] asbyte = new byte[4];
    //Endianness, be careful! ouch!
    asbyte[0] = byte.Parse( splitted[3] );
    asbyte[1] = byte.Parse( splitted[2] );
    asbyte[2] = byte.Parse( splitted[1] );
    asbyte[3] = byte.Parse( splitted[0] );
    //shift of the byte to form the long value
    long asLong = (asbyte[0] << 24 ) | (asbyte[1] << 16 ) | ( asbyte[2] << 8 ) | asbyte[3];
    IPAddress ipad = new IPAddress( asLong );
    IPEndPoint ipe = new IPEndPoint(ipad,49922);
    udp.Connect(ipe);
    udp.Send(message_to_Send, message_to_Send.Length);
    udp.Close();
    }
    --------------------------------

    Thank you very much for your attention,



  • Bharathi

    You shouldn't have to go to the lengths you went to in order to get the IPAddress directly. Why couldn't you do this instead of the bit shifting, etc

    IPAddress ipad = IPAddress.Parse("10.148.203.86");



  • UDPClient Slow Connection