I profiled some boilerplate UDP code and the speed was very good for sending only some small amount data (which is my intent).
BUT the "connect" method is "very slow" compared to the "send" method.
This takes 50 - 80 ms:
udpClient = new UdpClient();
udpClient.Connect("HOSTNAME", 11000);
Sending is then hardly profiled by 1 ms, because its so amazing fast:
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
udpClient.Send(sendBytes, sendBytes.Length);
I wonder what this "connect" method does, since UDP is connectionless by design.
If I leave out the connect method, then send is slower per call:
udpClient.Send(sendBytes, sendBytes.Length,"HOSTNAME",1100);
Any chance to improve the "connect" speed?
Disclaimer: I know UDP is unreliable, but for my app (client statistics, which are not 100% exact anyway) it doesn't matter if packages come in wrong order and even some lost packages don't kill me.
udpClient.Connect sets the default host name and port, so subsequently you can call Send without specifying them. What's taking the time is the name resolution - translating HOSTNAME into an IPAddress.
If you do it once in Connect, you don't have to do it every time you Send, which is faster.
I have come to the following conlusion:
There is no way to make UDP faster with C#. UdpClient with standard MSDN samples is the best choice.
I found a wrapper for the core API of winsock: http://www.elitepvpers.com/forum/co2-programming/159327-advanced-winsock-c.html
But when you hit "connect" (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx) the speed is rougly the same as with the managed UdpClient.