TCPMaid 5.0.0
See the version list below for details.
dotnet add package TCPMaid --version 5.0.0
NuGet\Install-Package TCPMaid -Version 5.0.0
<PackageReference Include="TCPMaid" Version="5.0.0" />
paket add TCPMaid --version 5.0.0
#r "nuget: TCPMaid, 5.0.0"
// Install TCPMaid as a Cake Addin #addin nuget:?package=TCPMaid&version=5.0.0 // Install TCPMaid as a Cake Tool #tool nuget:?package=TCPMaid&version=5.0.0
TCPMaid
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 (TLS) encryption and certificates
- Automatically serialises messages to bytes
- Automatically fragments large messages to avoid congestion
- Supports requests and responses
- Supports IPv4 and IPv6
Dependencies
Example
Client
public static async void Client() {
// Connect to server
using ClientMaid Client = new();
await Client.ConnectAsync("localhost", 5000);
// Say hello to server
await Client.Channel!.SendAsync(new ExampleMessage("hello server!"));
// Disconnect from server
await Client.Channel!.DisconnectAsync();
}
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(long Id, string ExampleText) : Message(Id) {
public readonly string ExampleText = ExampleText;
}
Output
Here's my response: -.-
Encryption
To encrypt the connection, simply pass an X509Certificate2
to ServerMaidOptions
and enable Ssl
in ClientMaidOptions
.
An SslStream
(TLS) is automatically created and authenticated.
Client
ClientMaid ClientMaid = new(new ClientMaidOptions() {
Ssl = true,
ServerName = "example.com" // Optional
});
Server
ServerMaid ServerMaid = new(new ServerMaidOptions() {
Certificate = X509Certificate2.CreateFromPemFile(
certPemFilePath: "/etc/letsencrypt/live/example.com/fullchain.pem",
keyPemFilePath: "/etc/letsencrypt/live/example.com/privkey.pem"
)
});
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 | 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. |
-
net8.0
- MemoryPack (>= 1.21.3)
-
net9.0
- MemoryPack (>= 1.21.3)
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 | |
---|---|---|---|
6.0.0 | 0 | 1/27/2025 | |
5.0.0 | 32 | 1/25/2025 | |
4.0.0 | 31 | 1/25/2025 | |
3.0.0 | 99 | 12/7/2024 | |
2.4.1 | 154 | 4/13/2024 | |
2.4.0 | 114 | 3/31/2024 | |
2.3.0 | 111 | 3/30/2024 | |
2.2.0 | 122 | 3/29/2024 | |
2.1.1 | 135 | 3/17/2024 | |
2.1.0 | 134 | 3/7/2024 | |
2.0.4 | 141 | 3/3/2024 | |
1.0.7 | 123 | 2/22/2024 | |
1.0.6 | 131 | 2/20/2024 | |
1.0.5 | 144 | 2/18/2024 | |
1.0.4 | 149 | 2/17/2024 | |
1.0.3 | 142 | 2/7/2024 | |
1.0.2 | 136 | 2/5/2024 | |
1.0.1 | 130 | 1/22/2024 |