SimpleTcpLib 1.0.0

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

// Install SimpleTcpLib as a Cake Tool
#tool nuget:?package=SimpleTcpLib&version=1.0.0

SimpleTcp

A simple-to-use TCP server and client library.

Examples

Echo Server

static void Main(string[] args)
{
    using (RawTcpServer tcpServer = new RawTcpServer())
    {
        tcpServer.ClientConnected += (sender, e) =>
            Console.WriteLine($"[{e.Client}]: Connected"); // new client connected
        tcpServer.ClientDisconnected += (sender, e) =>
            Console.WriteLine($"[{e.Client}]: Disconnected"); // client disconnected
        tcpServer.DataReceived += (sender, e) =>
        {
            byte[] readBytes = e.Client.ReadExisting(); // read all data
            string dataString = readBytes.Aggregate( // data to hex string
                new StringBuilder(32),
                (stringBuilder, data) => stringBuilder.Append($" 0x{data.ToString("X2")}")
                ).ToString().Trim();

            Console.WriteLine($"[{e.Client}]: {dataString}");

            e.Client.Write(readBytes, 0, readBytes.Length); // return same data
        };

        try
        {
            tcpServer.Start(5000);
            Console.WriteLine("Listening for connections...");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

        Console.ReadLine();
    }
}

Echo Client

static void Main(string[] args)
{
    using (RawTcpClient tcpClient = new RawTcpClient())
    {
        tcpClient.Connected += (sender, e) =>
            Console.WriteLine($"Connect to [{e.RemoteEndPoint}]");
        tcpClient.Disconnected += (sender, e) =>
            Console.WriteLine($"{Environment.NewLine}Disconnected from [{e.RemoteEndPoint}]");
        tcpClient.DataReceived += (sender, e) =>
        {
            if (sender is RawTcpClient rawTcpClient)
            {
                byte[] readBytes = rawTcpClient.ReadExisting(); // read all data
                Console.WriteLine($"DataReceived: {Encoding.ASCII.GetString(readBytes)}");
            }
        };

        try
        {
            tcpClient.Connect("127.0.0.1", 5000);

            while (true)
            {
                string line = Console.ReadLine();
                byte[] buffer = Encoding.ASCII.GetBytes(line);
                tcpClient.Write(buffer, 0, buffer.Length);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            Console.ReadLine();
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has 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
1.0.9 448 9/2/2021
1.0.8 275 8/30/2021
1.0.7 279 8/28/2021
1.0.6 286 8/27/2021
1.0.5 341 8/27/2021
1.0.4 396 8/27/2021
1.0.3 501 8/26/2021
1.0.2 589 8/26/2021
1.0.1 600 8/26/2021
1.0.0 625 8/25/2021

First release