best approach to sending high volumes of small UDP packets

34 views Asked by At

I have a Windows Service written in C# .NET that manages communications between many hundreds of endpoints, all with UDP (using a single assigned port). The Service can receive and process 100 or so messages per second, and needs to send small ACK packets back to the senders. There are many threads that need to send different types of UDP messages on this port, so I have been using ConcurrentQueue to organize the flow.

Under heavy traffic loads this system seems to be breaking down, and no UDP is sent for 4-5 minutes at a time every few days.

My question is, is it better to create a single UDPClient instance and queue outbound UDP messages, or can each process that needs to send a packet just create a transient UDP socket that immediately closes? Code would look like this:

string reply = "response";
using (var u = new UdpClient("192.168.99.60", 10655))
{
    byte[] buf = new byte[38];
    int index = 6;
    for (int i = 0; i < 6; i++) buf[i] = (byte)'>';
    byte[] resp = Encoding.ASCII.GetBytes(reply);
    Array.Copy(resp,0,buf,6,resp.Length);
    u.Send(buf, buf.Length);
}

instead of this (the method in use):

string reply = "response";
UdpSendBlock U = new UdpSendBlock(reply, "192.168.99.60");
UdpTXQ.Enqueue(U);

With the ConcurrentQueue method, a single thread continuously unwinds the queue and sends UDP packets one at a time (order is not important) to a single, established UDP socket that is created at startup.

Put another way, with the USING clause example shown above, is it practical to allow multiple threads to all send UDP packets with the same destination port, asynchronously, in high volume? Does the winsock stack manage all the multiple calls to Send()? In the worst case, I would need to send 50 to 100 UDP packets per second.

0

There are 0 answers