Demers.Packets 0.2.0

dotnet add package Demers.Packets --version 0.2.0
                    
NuGet\Install-Package Demers.Packets -Version 0.2.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="Demers.Packets" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Demers.Packets" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Demers.Packets" />
                    
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 Demers.Packets --version 0.2.0
                    
#r "nuget: Demers.Packets, 0.2.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 Demers.Packets@0.2.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=Demers.Packets&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Demers.Packets&version=0.2.0
                    
Install as a Cake Tool

Demers.packets

A packet based abstraction for TCP sockets, servers, and client.
Nuget: Demers.Packets

Installation

The package can be installed by adding the Nuget package Demers.Packets

Usage

Demers.Packets can be used in both a client and a server mode, it also provides packet readers and writers.

Packet Structure

All packets sent by this library are structured as follows:

  • Bytes 0 - 3 - Int, Opcode - Use the Opcode to determine the meaning of the packet in your application
  • Bytes 4 - 7 - Int, Length - The length of the packet's data
  • Bytes 8 - 8+Length - The data sent in the packet. Using the PacketWriter.WriteX methods the data will automatically be appended in order. User the PacketReader.ReadX methods in the same order to get the data back out.

Example Packets

An example packet might look like:

  • Structure:
  • Opcode = 1 - Enum in our application will determine Opcode 1 = MessageWithLocation
  • String - message,
  • Float - Latitude,
  • Float - Longitude

This would result in the writing of the packet like:

PacketWriter writer = new PacketWriter();
writer.NewPacket(Opcodes.MessageWithLocation); // 1
writer.WriteString($"Hello from Connecticut");
writer.WriteFloat(41.846);
writer.WriteFloat(-72.454)

And it would be read like:

PacketReader reader = new PacketReader(packet);
switch(packet.Opcode)
{
    case Opcodes.MessageWithLocation:
        Console.WriteLine(reader.ReadString());
        Console.Writeline($"{reader.ReadFloat()},{reader.ReadFloat()}");
        break;
    
    default: break;
}

Server Usage

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Demers.Packets;

Console.WriteLine("Server listening on 8888. Press Q to quit.");

var server  = new PacketServer(8888);
var clients = new List<PacketClient>();

while (true)
{
    // Accept new clients (non-blocking)
    var newClient = server.Accept();
    if (newClient != null)
    {
        Console.WriteLine("Client connected");
        newClient.OnReceive += (packet, client) =>
        {
            var reader = new PacketReader(packet);
            Console.WriteLine(
                $"Opcode={packet.Opcode}, Len={packet.Length}, Data='{reader.ReadString()}'"
            );
        };
        clients.Add(newClient);
    }

    // Poll clients to process incoming packets; remove any that disconnect
    for (int i = clients.Count - 1; i >= 0; i--)
    {
        if (!await clients[i].ReadAsync())
        {
            Console.WriteLine("Client disconnected");
            clients.RemoveAt(i);
        }
    }

    if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Q) break;
    await Task.Delay(10);
}

Client Usage

using System;
using System.Threading.Tasks;
using Demers.Packets;

Console.WriteLine("Client connecting to 127.0.0.1:8888. Press Q to quit.");

var client = new PacketClient("127.0.0.1", 8888);

// Optional: handle any server packets
client.OnReceive += (packet, thisClient) =>
{
    var reader = new PacketReader(packet);
    Console.WriteLine($"Opcode={packet.Opcode}"");
    Console.WriteLine($"Server says: '{reader.ReadString()}'");
};

var lastSend = DateTime.UtcNow;

while (true)
{
    // Process incoming data; exit if connection drops
    if (!await client.ReadAsync())
    {
        Console.WriteLine("Disconnected.");
        break;
    }

    // Send a simple packet every 2 seconds
    if (DateTime.UtcNow - lastSend >= TimeSpan.FromSeconds(2))
    {
        var writer = new PacketWriter();
        writer.NewPacket(1);    //Mock Opcode
        writer.WriteString("Hello from client!");
        await client.WriteAsync(writer.GetPacket());
        lastSend = DateTime.UtcNow;
        Console.WriteLine("Sent packet.");
    }

    if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Q) break;
    await Task.Delay(10);
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

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
0.2.0 229 9/3/2025
0.1.0 385 9/30/2022

v0.2.0
         - Added async read and write methods
         - Added .net 8.0 and .net 9.0 support
         - Added examples to README

         v0.1.0
         - Initial release of Demers.Packets