UDP Communication Aspect

Hi, Today I would like to show you UDP Communication Aspect. You can consider test that is shown below. It is performance measurement of my last blog entry about communication via UDP over the IP network. And that is very simple Console Application.

public class Program
{
  static void Main() {
    var serverIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);
    var clientIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 2000);
    var udpComunicationServer = new UdpCommunicationServer(serverIpEndPoint);
    var udpComunicationClient = new UdpCommunicationClient(clientIpEndPoint);

    var packetCount = 0;

    udpComunicationServer.DatagramReceived +=
      datagram => Interlocked.Increment(ref packetCount);

    Console.WriteLine(@"Start sending.");

    var stopwatch = Stopwatch.StartNew();
    for (int i = 0; i < 200000; i++)
    {
      udpComunicationClient
        .SendData(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, serverIpEndPoint);
      udpComunicationClient
        .SendData(new byte[] { 2, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, serverIpEndPoint);
      udpComunicationClient
        .SendData(new byte[] { 3, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, serverIpEndPoint);
      udpComunicationClient
        .SendData(new byte[] { 4, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, serverIpEndPoint);
      udpComunicationClient
        .SendData(new byte[] { 5, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, serverIpEndPoint);
    }
    stopwatch.Stop();

    Thread.Sleep(4);

    Console.WriteLine(@"Sending {0} tivial UDP packets takes about {1} ms.",
      packetCount,
      stopwatch.ElapsedMilliseconds);

    Console.ReadKey();
  }
}

Ok so, I will show you what is about the worst thing I show you last time. Do you remember that I changed my solutions a bit to something worst? That is code of modification for remember.

[MethodImpl(MethodImplOptions.Synchronized)]
public bool SendData(byte[] datagram, IPEndPoint serverIpEndPoint) {
  lock (this) {
    while (!udpClient.Client.Poll(100, SelectMode.SelectWrite))
      ;
    return datagram.Length == udpClient.Send(
      datagram, datagram.Length,
      serverIpEndPoint);
  }
}
private void StartUdpListener() {
  do {
    IPEndPoint ipEndPoint = null;
    var datagram = udpClientListener.Receive(ref ipEndPoint);
    DatagramRecived(datagram);
  } while (true);
}
private void StartUdpListener() {
  do {
    IPEndPoint ipEndPoint = null;
    var asyncResult = udpClientListener.BeginReceive(null, null);
    asyncResult.AsyncWaitHandle.WaitOne(Timeout.Infinite);
    var datagram = udpClientListener.EndReceive(asyncResult, ref ipEndPoint);
    DatagramRecived(datagram);
  } while (true);
}
private void StartUdpListener() {
  do {
    IPEndPoint ipEndPoint = null;
    var asyncResult = udpClientListener.BeginReceive(null, null);
    asyncResult.AsyncWaitHandle.WaitOne(Timeout.Infinite);
    var datagram = udpClientListener.EndReceive(asyncResult, ref ipEndPoint);
    DatagramRecived(datagram);
  } while (true);
}

The pattern I would like to show you to remember is something like is shown below.

var asyncResult = udpClientListener.BeginReceive(null, null);
asyncResult.AsyncWaitHandle.WaitOne(Timeout.Infinite);
var datagram = udpClientListener.EndReceive(asyncResult, ref ipEndPoint);

There is no callback and no state, I put null values as a parameters. Instead of that I wait with AsyncWaitHandle with infinite timeout.

Regards,

P ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.