SimpleTcpLib 1.0.6

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

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

SimpleTcp

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

Usage

Start Tcp Server

RawTcpServer tcpServer = new RawTcpServer();
tcpServer.Start(5000);

Can use Event Handlers

RawTcpServer tcpServer = new RawTcpServer();
tcpServer.ClientConnected += ClientConnected;
tcpServer.ClientDisconnected += ClientDisconnected;
tcpServer.DataReceived += DataReceived;

Connect to Tcp Server

RawTcpClient tcpClient = new RawTcpClient();
tcpClient.Connect("127.0.0.1", 5000);

Can use Event Handlers

RawTcpClient tcpClient = new RawTcpClient();
tcpClient.Connected += Connected;
tcpClient.Disconnected += Disconnected;
tcpClient.DataReceived += DataReceived;

Examples

Echo Server

static void Main(string[] args)
{
    using (RawTcpServer tcpServer = new RawTcpServer())
    {
        tcpServer.ClientConnected += (sender, e) =>
            Console.WriteLine($"[{e}]: Connected"); // new client connected
        tcpServer.ClientDisconnected += (sender, e) =>
            Console.WriteLine($"[{e}]: 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();
        }
    }
}

Packet Echo Server

The size of the received data is received without being cut off.

static void Main(string[] args)
{
  using (PacketTcpServer tcpServer = new PacketTcpServer())
  {
      tcpServer.ClientConnected += (sender, e) =>
          Console.WriteLine($"[{e}]: Connected"); // new client connected
      tcpServer.ClientDisconnected += (sender, e) =>
          Console.WriteLine($"[{e}]: Disconnected"); // client disconnected
      tcpServer.PacketReceived += (sender, e) =>
      {
          if (sender is PacketTcpServer packetTcpServer)
          {
              Console.WriteLine($"[{e.Packet.IPEndPoint}]: PacketReceived (PacketLength: {e.Packet.PacketData.Length})");
              packetTcpServer.WritePacket(e.Packet.TcpClient, e.Packet.PacketData); // return same packet
          }
      };

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

      Console.ReadLine();
  }
}

Packet Echo Client

static void Main(string[] args)
{
    using (PacketTcpClient tcpClient = new PacketTcpClient())
    {
        tcpClient.Connected += (sender, e) =>
            Console.WriteLine($"Connect to [{e.RemoteEndPoint}]");
        tcpClient.Disconnected += (sender, e) =>
            Console.WriteLine($"{Environment.NewLine}Disconnected from [{e.RemoteEndPoint}]");
        tcpClient.PacketReceived += (sender, e) =>
        {
            Console.WriteLine($"PacketReceived: (PacketLength: {e.PacketData.Length})");
        };

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

            tcpClient.WritePacket(new byte[1024]); // send 1024 bytes
            tcpClient.WritePacket(new byte[1024 * 1024]); // send 1024 * 1024 bytes
            tcpClient.WritePacket(new byte[1024 * 1024 * 10]); // send 1024 * 1024 * 10 bytes

            Console.ReadLine();

            tcpClient.Disconnect();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

        Console.ReadLine();
    }
}

image

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 was computed.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.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.

Fixed an issue where an exception was thrown in the server's Accept process when the server shuts down.