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.

UDPClient Slow Connection
RobGMiller
epilotusa
find out where you are getting the delay. Please post this sample
and we will take a look
Shem1
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
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
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");