TCPMaid 2.4.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TCPMaid --version 2.4.1                
NuGet\Install-Package TCPMaid -Version 2.4.1                
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="TCPMaid" Version="2.4.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TCPMaid --version 2.4.1                
#r "nuget: TCPMaid, 2.4.1"                
#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.
// Install TCPMaid as a Cake Addin
#addin nuget:?package=TCPMaid&version=2.4.1

// Install TCPMaid as a Cake Tool
#tool nuget:?package=TCPMaid&version=2.4.1                

Icon

TCPMaid

NuGet

An easy, powerful and lightweight TCP client/server in C#.

TCPMaid makes it easy to setup a robust client & server, send messages and requests, and provide your own SSL certificate.

Features

  • Easy client & server setup
  • Supports SSL encryption and certificates
  • Automatically serialises messages
  • Automatically fragments large messages
  • Supports requests and responses
  • Supports IPv4 and IPv6

Dependencies

Example

Client
public static async void Client() {
    // Connect client to server
    ClientMaid Client = new();
    await Client.ConnectAsync("localhost", 5000);

    // Say hello to server
    await Client.Channel!.SendAsync(new ExampleMessage("hello server!"));
}
Server
public static void Server() {
    // Start server on port 5000
    ServerMaid Server = new();
    Server.Start(5000);

    // Listen to events
    Server.OnConnect += OnConnect;
    Server.OnReceive += OnReceive;

    // Events
    void OnConnect(Channel Client) {
        Console.WriteLine("Hi, client!");
    }
    void OnReceive(Channel Client, Message Message) {
        if (Message is ExampleMessage ExampleMessage) {
            Console.WriteLine($"Received '{ExampleMessage.ExampleText}' from client!");
        }
    }
}
Shared
[MemoryPackable]
public partial class ExampleMessage(string ExampleText) : Message {
    public readonly string ExampleText = ExampleText;
}
Output
Hi, client!
Received 'hello server!' from client!

Requests

The client may want to ask the server for data. To send a message and wait for a response, you can use RequestAsync.

Client
// Send an ExampleRequest and wait for an ExampleResponse with the same message ID
ExampleResponse? Response = await Client.Channel!.RequestAsync<ExampleResponse>(new ExampleRequest());
Console.WriteLine(Response!.ExampleText);
Server
Server.OnReceive += (Channel, Message) => {
    if (Message is ExampleRequest ExampleRequest) {
        _ = Channel.SendAsync(new ExampleResponse(ExampleRequest.ID, "Here's my response: -.-"));
    }
};
Shared
[MemoryPackable]
public partial class ExampleRequest : Message {
}
[MemoryPackable]
public partial class ExampleResponse(ulong ID, string ExampleText) : Message(ID) {
    public readonly string ExampleText = ExampleText;
}
Output
Here's my response: -.-

Streams

You may want to send a large file without keeping it all in memory. To send data from a stream, you can use SendStreamAsync and ReceiveStreamAsync.

Sender
using FileStream Reader = File.OpenRead("Cat.png");
await Channel.SendStreamAsync("Cat Picture", Reader);
Receiver
using FileStream Writer = File.OpenWrite("Cat.png");
await Channel.ReceiveStreamAsync("Cat Picture", Writer);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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. 
.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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.