NTDLS.DatagramMessaging
                             
                            
                                1.7.5
                            
                        
                    dotnet add package NTDLS.DatagramMessaging --version 1.7.5
NuGet\Install-Package NTDLS.DatagramMessaging -Version 1.7.5
<PackageReference Include="NTDLS.DatagramMessaging" Version="1.7.5" />
<PackageVersion Include="NTDLS.DatagramMessaging" Version="1.7.5" />
<PackageReference Include="NTDLS.DatagramMessaging" />
paket add NTDLS.DatagramMessaging --version 1.7.5
#r "nuget: NTDLS.DatagramMessaging, 1.7.5"
#:package NTDLS.DatagramMessaging@1.7.5
#addin nuget:?package=NTDLS.DatagramMessaging&version=1.7.5
#tool nuget:?package=NTDLS.DatagramMessaging&version=1.7.5
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 and compression with optional overloads.
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()
{
    var dmServer = new DmServer(1234);
    //Typically a good idea to hook the exception handler.
    //dmServer.OnException += DmServer_OnException;
    dmServer.OnDatagramReceived += UdpManager_OnDatagramReceived;
    Console.ReadLine();
    dmServer.Stop();
}
private static void UdpManager_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 dmServer = new DmServer(1234);
    //Typically a good idea to hook the exception handler.
    //dmServer.OnException += DmServer_OnException;
    dmServer.AddHandler(new HandlePackets());
    Console.ReadLine();
    dmServer.Stop();
}
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 a listen port. This means that this this manager is in write-only mode. We are going to loop and send frames containing serialized MyFirstUDPPacket.
static void Main()
{
    var dmClient = new DmClient("127.0.0.1", 1234);
    //Typically a good idea to hook the exception handler.
    //dmClient.OnException += DmServer_OnException;
    int packetNumber = 0;
    while (true)
    {
        dmClient.Dispatch(new MyFirstUDPPacket($"Packet#:{packetNumber++} "));
        Thread.Sleep(10);
    }
    Console.ReadLine();
    dmClient.Stop();
}
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 | Versions Compatible and additional computed target framework versions. | 
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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 was computed. 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. | 
- 
                                                    
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.10)
 - Microsoft.Extensions.Caching.Memory (>= 9.0.10)
 - NTDLS.Semaphore (>= 3.5.3)
 - protobuf-net (>= 3.2.56)
 
 - 
                                                    
net9.0
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.10)
 - Microsoft.Extensions.Caching.Memory (>= 9.0.10)
 - NTDLS.Semaphore (>= 3.5.3)
 - protobuf-net (>= 3.2.56)
 
 
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | 
|---|---|---|
| 1.7.5 | 165 | 10/27/2025 | 
| 1.7.4 | 228 | 5/5/2025 | 
| 1.7.3 | 186 | 5/5/2025 | 
| 1.7.2 | 136 | 5/2/2025 | 
| 1.7.1 | 216 | 3/11/2025 | 
| 1.7.0 | 213 | 3/11/2025 | 
| 1.6.2 | 240 | 3/10/2025 | 
| 1.6.1 | 210 | 3/10/2025 | 
| 1.6.0 | 227 | 3/10/2025 | 
| 1.5.2 | 211 | 3/10/2025 | 
| 1.5.1 | 208 | 3/10/2025 | 
| 1.5.0 | 207 | 3/10/2025 | 
| 1.4.0 | 264 | 3/8/2025 | 
| 1.3.7 | 141 | 3/2/2025 | 
| 1.3.6 | 124 | 1/8/2025 | 
| 1.3.5 | 166 | 1/4/2025 | 
| 1.3.4 | 164 | 1/4/2025 | 
| 1.3.3 | 161 | 12/31/2024 | 
| 1.3.2 | 150 | 12/3/2024 | 
| 1.3.1 | 333 | 2/19/2024 | 
| 1.3.0 | 301 | 2/15/2024 | 
| 1.2.0 | 310 | 2/15/2024 | 
Dependency updates.