NTDLS.DatagramMessaging 1.9.0

dotnet add package NTDLS.DatagramMessaging --version 1.9.0
                    
NuGet\Install-Package NTDLS.DatagramMessaging -Version 1.9.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NTDLS.DatagramMessaging" Version="1.9.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NTDLS.DatagramMessaging" Version="1.9.0" />
                    
Directory.Packages.props
<PackageReference Include="NTDLS.DatagramMessaging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NTDLS.DatagramMessaging --version 1.9.0
                    
#r "nuget: NTDLS.DatagramMessaging, 1.9.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NTDLS.DatagramMessaging@1.9.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NTDLS.DatagramMessaging&version=1.9.0
                    
Install as a Cake Addin
#tool nuget:?package=NTDLS.DatagramMessaging&version=1.9.0
                    
Install as a Cake Tool

NTDLS.DatagramMessaging

📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.DatagramMessaging

NTDLS.DatagramMessaging is a set of classes and extensions methods that allow you to send/receive UDP packets with ease. It handles corruption checks, concatenation, fragmentation, serialization, compression and encryption.

UDP Sever (Event based):

Here we are instantiating a DmMessenger and giving it a listen port. This will cause the manager to go into listen mode. Any received messages will handled by the OnDatagramReceived event.

static void Main()
{
    using var messenger = new DmMessenger(1234);

    messenger.OnDatagramReceived += messenger_OnDatagramReceived;

    Console.WriteLine("Press enter to quit.");
    Console.ReadLine();
}

private static void Messenger_OnDatagramReceived(DmContext context, IDmDatagram datagram)
{
    if (datagram is MyFirstUDPPacket myFirstUDPPacket)
    {
        context.Dispatch(myFirstUDPPacket); //Echo the datagram back to the sender.
        Console.WriteLine($"{myFirstUDPPacket.Message}->{myFirstUDPPacket.UID}->{myFirstUDPPacket.TimeStamp}");
    }
}

UDP Sever (Convention based):

Here we are instantiating a DmMessenger and giving it a listen port. This will cause the manager to go into listen mode. Any received messages will handled by the class HandlePackets which was suppled to the UDP messenger by a call to AddHandler().

static void Main()
{
    var messenger = new DmMessenger(1234);

    messenger.AddHandler(new HandlePackets());

    Console.WriteLine("Press enter to quit.");
    Console.ReadLine();
}

private class HandlePackets : IDmMessageHandler
{
    private class HandlePackets : IDmMessageHandler
    {
        public static void DatagramHandler(DmContext context, MyFirstUDPPacket datagram)
        {
            context.Dispatch(datagram); //Echo the datagram back to the sender.
            Console.WriteLine($"{datagram.Message}->{datagram.UID}->{datagram.TimeStamp}");
        }
    }
}

UDP Client:

Here we are instantiating a DmMessenger without a listen port. This means that this this messenger is in write-only mode. We are going to loop and send frames containing serialized MyFirstUDPPacket. Note that the we could also specify a handler for convention based message handling.

static void Main()
{
    using var messenger = new DmMessenger();

    messenger.OnDatagramReceived += messenger_OnDatagramReceived;

    int packetNumber = 0;

    var endpointCtx = messenger.GetEndpointContext("127.0.0.1", 1234);

    while (true)
    {
        messenger.Dispatch(new MyFirstUDPPacket($"Packet#:{packetNumber++} "), endpointCtx);
        Thread.Sleep(10);
    }

    Console.WriteLine("Press enter to quit.");
    Console.ReadLine();
}

Supporting Code:

The class that we are going to be serializing and deserializing in the examples.

public class MyFirstUDPPacket : IDmDatagram
{
    public DateTime TimeStamp { get; set; } = DateTime.UtcNow;
    public Guid UID { get; set; } = Guid.NewGuid();
    public string Message { get; set; } = string.Empty;

    public MyFirstUDPPacket()
    {
    }

    public MyFirstUDPPacket(string message)
    {
        Message = message;
    }
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on NTDLS.DatagramMessaging:

Package Downloads
Ae.MpClientToServerComms

Axis Engine is a 2D game engine written in C# and rendered with DirectX.

Ae.MpCommsMessages

Axis Engine is a 2D game engine written in C# and rendered with DirectX.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.9.0 108 2/7/2026
1.8.1 92 2/5/2026
1.8.0 93 2/5/2026
1.7.6 305 11/14/2025
1.7.5 209 10/27/2025
1.7.4 257 5/5/2025
1.7.3 221 5/5/2025
1.7.2 166 5/2/2025
1.7.1 243 3/11/2025
1.7.0 250 3/11/2025
1.6.2 278 3/10/2025
1.6.1 246 3/10/2025
1.6.0 260 3/10/2025
1.5.2 243 3/10/2025
1.5.1 242 3/10/2025
1.5.0 238 3/10/2025
1.4.0 296 3/8/2025
1.3.7 172 3/2/2025
1.3.6 157 1/8/2025
1.3.5 195 1/4/2025
Loading failed

Major changes to server/client architecture to support better scalability and maintainability.