SuperSimpleTcp 3.0.17

dotnet add package SuperSimpleTcp --version 3.0.17                
NuGet\Install-Package SuperSimpleTcp -Version 3.0.17                
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="SuperSimpleTcp" Version="3.0.17" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SuperSimpleTcp --version 3.0.17                
#r "nuget: SuperSimpleTcp, 3.0.17"                
#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 SuperSimpleTcp as a Cake Addin
#addin nuget:?package=SuperSimpleTcp&version=3.0.17

// Install SuperSimpleTcp as a Cake Tool
#tool nuget:?package=SuperSimpleTcp&version=3.0.17                

supersimpletcp

SuperSimpleTcp

Simple wrapper for TCP client and server in C# with SSL support

NuGet Version NuGet

SuperSimpleTcp provides simple methods for creating your own TCP-based sockets application, enabling easy integration of connection management, sending, and receiving data.

I would highly encourage you to fully understand what message framing is and why it's important before using this library: https://blog.stephencleary.com/2009/04/message-framing.html

New in v3.0.x

  • Breaking change, allocation-free receive (thank you @joreg)
  • Configurable sync vs async for firing DataReceived events (thank you @TheNybbler)
  • More configurability around certificate validation checks (thank you @ATS-CE)
  • Better catch client timeouts during TLS establishment (thank you @ATS-CE)
  • Add NoDelay to settings (thank you @huangjia2107)
  • Fix for PollSocket method, thank you @zllvm @Energiz0r @Espen-Kalhagen-Element-Logic
  • Added server-side NoDelay property in settings, thank you @QTPah

Special Thanks

A special thanks to the community of people that have contributed to or otherwise improved this project!

@tinohager @u1035 @cmeeren @pha3z @opnop @kopkarmecoindo @simonhaines @matt1tk @lukeacat @exergist @maynardsi @sector13371 @loganwoodxyz @jwfxpr @IanPNewson @EGirardi @redrabbit007 @eatyouroats @joreg @CetinOzdil @tautvilis @ATS-CE @TheNybbler @huangjia2107 @zllvm @Energiz0r @Espen-Kalhagen-Element-Logic @MarkBreedveld @QTPah @olifer @KimEoJin @BrandenEK @Somfic

Help or Feedback

Need help or have feedback? Please file an issue here!

Simple Examples

Server Example

using SuperSimpleTcp;

void Main(string[] args)
{
  // instantiate
  SimpleTcpServer server = new SimpleTcpServer("127.0.0.1:9000");

  // set events
  server.Events.ClientConnected += ClientConnected;
  server.Events.ClientDisconnected += ClientDisconnected;
  server.Events.DataReceived += DataReceived;

  // let's go!
  server.Start();

  // once a client has connected...
  server.Send("[ClientIp:Port]", "Hello, world!");
  Console.ReadKey();
}

static void ClientConnected(object sender, ConnectionEventArgs e)
{
  Console.WriteLine($"[{e.IpPort}] client connected");
}

static void ClientDisconnected(object sender, ConnectionEventArgs e)
{
  Console.WriteLine($"[{e.IpPort}] client disconnected: {e.Reason}");
}

static void DataReceived(object sender, DataReceivedEventArgs e)
{
  Console.WriteLine($"[{e.IpPort}]: {Encoding.UTF8.GetString(e.Data.Array, 0, e.Data.Count)}");
}

Client Example

using SuperSimpleTcp;

void Main(string[] args)
{
  // instantiate
  SimpleTcpClient client = new SimpleTcpClient("127.0.0.1:9000");

  // set events
  client.Events.Connected += Connected;
  client.Events.Disconnected += Disconnected;
  client.Events.DataReceived += DataReceived;

  // let's go!
  client.Connect();

  // once connected to the server...
  client.Send("Hello, world!");
  Console.ReadKey();
}

static void Connected(object sender, ConnectionEventArgs e)
{
  Console.WriteLine($"*** Server {e.IpPort} connected");
}

static void Disconnected(object sender, ConnectionEventArgs e)
{
  Console.WriteLine($"*** Server {e.IpPort} disconnected"); 
}

static void DataReceived(object sender, DataReceivedEventArgs e)
{
  Console.WriteLine($"[{e.IpPort}] {Encoding.UTF8.GetString(e.Data.Array, 0, e.Data.Count)}");
}

Connect With Retries

The ConnectWithRetries method on SimpleTcpClient can be used instead of Connect to continually attempt to establish connections with the server for a given period of time. Like Connect, ConnectWithRetries will throw a TimeoutException if it is unable to successfully establish a connection.

client.ConnectWithRetries(10000); // try for up to 10000 milliseconds

Additional Configuration Options

Both SimpleTcpClient and SimpleTcpServer have settable values for:

  • Logger - method to invoke to send log messages from either SimpleTcpClient or SimpleTcpServer
  • Settings.MutuallyAuthenticate - only used if SSL is enabled, demands that both client and server mutually authenticate
  • Settings.AcceptInvalidCertificates - accept and allow certificates that are invalid or cannot be validated
  • Keepalive - to enable/disable keepalives and set specific parameters (disabled by default)

SimpleTcpServer also has:

  • Settings.IdleClientTimeoutSeconds - automatically disconnect a client if data is not received within the specified number of seconds

Additionally, both SimpleTcpClient and SimpleTcpServer offer a statistics object under SimpleTcpClient.Statistics and SimpleTcpServer.Statistics. These values (other than start time and uptime) can be reset using the Statistics.Reset() API.

Local vs External Connections

IMPORTANT

  • If you specify 127.0.0.1 as the listener IP address, it will only be able to accept connections from within the local host.
  • To accept connections from other machines:
    • Use a specific interface IP address, or
    • Use null, *, +, or 0.0.0.0 for the listener IP address (requires admin privileges to listen on any IP address)
  • Make sure you create a permit rule on your firewall to allow inbound connections on that port
  • If you use a port number under 1024, admin privileges will be required

Testing with SSL

A certificate named simpletcp.pfx is provided for simple testing. It should not expire for a really long time. It's a self-signed certificate and you should NOT use it in production. Its export password is simpletcp.

Disconnection Handling

The project TcpTest (https://github.com/jchristn/TcpTest) was built specifically to provide a reference for SuperSimpleTcp to handle a variety of disconnection scenarios. The disconnection tests for which SimpleTcp is evaluated include:

Test case Description Pass/Fail
Server-side dispose Graceful termination of all client connections PASS
Server-side client removal Graceful termination of a single client PASS
Server-side termination Abrupt termination due to process abort or CTRL-C PASS
Client-side dispose Graceful termination of a client connection PASS
Client-side termination Abrupt termination due to a process abort or CTRL-C PASS
Network interface down Network interface disabled or cable removed Partial (see below)

Additionally, as of v2.1.0, support for TCP keepalives has been added to SimpleTcp, primarily to address the issue of a network interface being shut down, the cable unplugged, or the media otherwise becoming unavailable. It is important to note that keepalives are supported in .NET Core and .NET Framework, but NOT .NET Standard. As of this release, .NET Standard provides no facilities for TCP keepalives.

TCP keepalives are disabled by default. To enable them:

server.Keepalive.EnableTcpKeepAlives = true;
server.Keepalive.TcpKeepAliveInterval = 5;      // seconds to wait before sending subsequent keepalive
server.Keepalive.TcpKeepAliveTime = 5;          // seconds to wait before sending a keepalive
server.Keepalive.TcpKeepAliveRetryCount = 5;    // number of failed keepalive probes before terminating connection

Some important notes about TCP keepalives:

  • Keepalives only work in .NET Core and .NET Framework
  • Keepalives can be enabled on either client or server, but are implemented and enforced in the underlying operating system, and may not work as expected
  • Keepalive.TcpKeepAliveRetryCount is only applicable to .NET Core; for .NET Framework, this value is forced to 10
  • Your mileage may vary; please remember that these are managed by the underlying operating system and not by this library

Running under Mono

.NET Core is the preferred environment for cross-platform deployment on Windows, Linux, and Mac. For those that use Mono, SimpleTcp should work well in Mono environments. It is recommended that you execute the containing EXE using --server and after using the Mono Ahead-of-Time Compiler (AOT).

mono --aot=nrgctx-trampolines=8096,nimt-trampolines=8096,ntrampolines=4048 --server myapp.exe
mono --server myapp.exe

Version History

Please refer to CHANGELOG.md.

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 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 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. 
.NET Framework net461 is compatible.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
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.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (12)

Showing the top 5 NuGet packages that depend on SuperSimpleTcp:

Package Downloads
ESCPOS_NET

.NET Standard 2.0 Implementation of ESC/POS command generation and integration with thermal printers. Allows creating receipts with all common functionality supported.

Vinci.Protocols.Communication

Package Description

Vinci.Protocols.Iec60875_104

Package Description

SaykalElectronicFramework

Common functions used in all projects have been combined.

HToolEx

This library provides MODBUS communication for connecting to and controlling Hantas tools.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on SuperSimpleTcp:

Repository Stars
lukevp/ESC-POS-.NET
Efficient, Easy to Use Thermal Printing & POS (Windows/Linux/OSX, WiFi/BT/USB/Ethernet)
Version Downloads Last updated
3.0.17 123 12/23/2024
3.0.14 32,438 1/16/2024
3.0.13 23,573 8/25/2023
3.0.12 7,370 7/13/2023
3.0.11 2,712 7/4/2023
3.0.10 57,869 2/24/2023
3.0.9 855 2/24/2023
3.0.8 836 2/24/2023
3.0.7 6,639 1/18/2023
3.0.6 12,634 1/2/2023
3.0.5 10,024 11/1/2022
3.0.4 1,262 10/28/2022
3.0.3 906 10/28/2022
3.0.1 995 10/28/2022
3.0.0.2 14,473 9/22/2022
3.0.0.1 7,828 9/2/2022
3.0.0 6,877 8/20/2022
2.6.1.4 1,950 8/16/2022
2.6.1.3 1,636 8/11/2022
2.6.1.2 1,602 8/9/2022
2.6.1.1 7,377 7/2/2022
2.6.1 9,367 7/1/2022
2.6.0.8 8,694 5/10/2022
2.6.0.7 5,666 3/29/2022
2.6.0.6 10,113 3/9/2022
2.6.0.5 4,342 2/23/2022
2.6.0.4 1,239 2/20/2022
2.6.0.1 1,101 2/18/2022
2.6.0 2,513 2/16/2022
2.5.0.4 1,637 2/7/2022
2.5.0.3 997 2/7/2022
2.5.0.2 8,015 1/19/2022
2.5.0.1 1,267 1/14/2022
2.5.0 1,266 1/13/2022
2.4.1.2 19,090 11/12/2021
2.4.1.1 17,984 8/17/2021
2.4.1 946 8/17/2021
2.4.0.2 2,273 8/11/2021
2.4.0 177,026 3/10/2021
2.3.0.2 1,220 3/6/2021
2.3.0.1 2,083 2/28/2021
2.3.0 1,128 2/28/2021
2.2.1.1 10,568 1/19/2021
2.2.1 5,041 12/25/2020
2.2.0.1 4,820 12/10/2020
2.2.0 1,244 12/9/2020
2.1.0.14 1,526 12/2/2020
2.1.0.13 3,772 11/28/2020
2.1.0.12 1,845 11/17/2020
2.1.0.11 1,256 11/15/2020
2.1.0.10 1,104 11/10/2020
2.1.0.9 1,104 11/10/2020
2.1.0.8 1,124 11/10/2020
2.1.0.7 3,128 10/10/2020
2.1.0.6 1,107 10/10/2020
2.1.0.4 1,086 10/9/2020
2.1.0.3 1,396 9/20/2020
2.1.0.2 1,139 9/17/2020
2.1.0.1 1,484 9/8/2020
2.1.0 2,491 9/1/2020
2.0.6.1 2,209 8/25/2020
2.0.6 19,254 8/11/2020
2.0.5 6,529 6/25/2020
2.0.4 34,746 3/12/2020
2.0.3 7,066 3/4/2020
2.0.2 7,570 2/24/2020
2.0.1 4,036 2/19/2020
1.1.8 5,025 2/16/2020
1.1.7 28,726 11/28/2019
1.1.6 14,933 11/7/2019
1.1.5 1,182 11/1/2019
1.1.4 1,705 9/21/2019
1.1.3 1,253 9/9/2019
1.1.0 1,296 9/6/2019
1.0.3 1,330 6/13/2019
1.0.2 1,381 3/11/2019
1.0.1 1,599 1/12/2019
1.0.0 1,654 11/29/2018

Await fix, thank you @olifer