Flare.Tcp 0.2.3

A basic multi-client message-based tcp server (and client).

Install-Package Flare.Tcp -Version 0.2.3
dotnet add package Flare.Tcp --version 0.2.3
<PackageReference Include="Flare.Tcp" Version="0.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Flare.Tcp --version 0.2.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Flare.Tcp, 0.2.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Flare.Tcp as a Cake Addin
#addin nuget:?package=Flare.Tcp&version=0.2.3

// Install Flare.Tcp as a Cake Tool
#tool nuget:?package=Flare.Tcp&version=0.2.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Flare.Tcp

nuget badge Unlicense

A basic multi-client message-based tcp server (and client).

Using the FlareTcpServer

// create a new server and start listening on port 4242
using var server = new FlareTcpServer();
server.Start(4242);

// accept a client and send the string "Test"
using var client = await server.AcceptClientAsync();
await client.WriteMessageAsync(Encoding.UTF8.GetBytes("You connected successfully!"));

// read the response and print it
using var message = await client.ReadNextMessageAsync();
Console.WriteLine(Encoding.UTF8.GetString(message.Span));

// stop listening.
server.Shutdown();

Using the FlareTcpClient

// create a new client and connect to localhost on port 4242
using var client = new FlareTcpClient();
await client.ConnectAsync(IPAddress.Loopback, 4242);

// send "Test" to the server and print the response
await client.WriteMessageAsync(Encoding.UTF8.GetBytes("Knock knock"));
using var message = await client.ReadNextMessageAsync();
Console.WriteLine(Encoding.UTF8.GetString(message.Span));

// disconnect from the server
client.Disconnect();

Using the ConcurrentFlareTcpServer

// create a new server
using var server = new ConcurrentFlareTcpServer();

// attach event handlers
server.ClientConnected += clientId => {
	Console.WriteLine($"Client {clientId} connected.");
};
server.ClientDisconnected += clientId => {
	Console.WriteLine($"Client {clientId} disconnected.");
	// stop the server
	server.Shutdown();
};
server.MessageReceived += (clientId, message) => {
	// echo message back to client and wait for it to be sent.
	server.EnqueueMessage(clientId, message);
};

// start listening on port 4242.
await server.ListenAsync(4242);

Using the ConcurrentFlareTcpClient

// create a new client
using var client = new ConcurrentFlareTcpClient();

// attach message callback
client.MessageReceived += message => {
	// print message to console
	Console.WriteLine(Encoding.UTF8.GetString(message.Span));
	// free the message buffer
	message.Dispose();
	// disconnect from server
	client.Disconnect();
};

// connect to localhost on port 4242
await client.ConnectAsync(IPAddress.Loopback, 4242);

// send test message
await client.EnqueueMessageAsync(Encoding.UTF8.GetBytes("Anyone there?"));

Icon

Icon made by Smashicons from flaticon.com

Flare.Tcp

nuget badge Unlicense

A basic multi-client message-based tcp server (and client).

Using the FlareTcpServer

// create a new server and start listening on port 4242
using var server = new FlareTcpServer();
server.Start(4242);

// accept a client and send the string "Test"
using var client = await server.AcceptClientAsync();
await client.WriteMessageAsync(Encoding.UTF8.GetBytes("You connected successfully!"));

// read the response and print it
using var message = await client.ReadNextMessageAsync();
Console.WriteLine(Encoding.UTF8.GetString(message.Span));

// stop listening.
server.Shutdown();

Using the FlareTcpClient

// create a new client and connect to localhost on port 4242
using var client = new FlareTcpClient();
await client.ConnectAsync(IPAddress.Loopback, 4242);

// send "Test" to the server and print the response
await client.WriteMessageAsync(Encoding.UTF8.GetBytes("Knock knock"));
using var message = await client.ReadNextMessageAsync();
Console.WriteLine(Encoding.UTF8.GetString(message.Span));

// disconnect from the server
client.Disconnect();

Using the ConcurrentFlareTcpServer

// create a new server
using var server = new ConcurrentFlareTcpServer();

// attach event handlers
server.ClientConnected += clientId => {
	Console.WriteLine($"Client {clientId} connected.");
};
server.ClientDisconnected += clientId => {
	Console.WriteLine($"Client {clientId} disconnected.");
	// stop the server
	server.Shutdown();
};
server.MessageReceived += (clientId, message) => {
	// echo message back to client and wait for it to be sent.
	server.EnqueueMessage(clientId, message);
};

// start listening on port 4242.
await server.ListenAsync(4242);

Using the ConcurrentFlareTcpClient

// create a new client
using var client = new ConcurrentFlareTcpClient();

// attach message callback
client.MessageReceived += message => {
	// print message to console
	Console.WriteLine(Encoding.UTF8.GetString(message.Span));
	// free the message buffer
	message.Dispose();
	// disconnect from server
	client.Disconnect();
};

// connect to localhost on port 4242
await client.ConnectAsync(IPAddress.Loopback, 4242);

// send test message
await client.EnqueueMessageAsync(Encoding.UTF8.GetBytes("Anyone there?"));

Icon

Icon made by Smashicons from flaticon.com

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.2.3 86 7/9/2021
0.2.2 68 4/1/2021
0.2.1 90 2/15/2021
0.2.0 58 2/15/2021
0.1.0 128 12/2/2020