Coder Social home page Coder Social logo

simpletcp's Introduction

alt tag

SimpleTcp

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

NuGet Version NuGet

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

New in v2.1.0

  • Breaking changes
  • Retarget to include .NET Core (includes previous targeting to .NET Standard and .NET Framework)
  • Consolidated settings and event classes
  • Added support for TCP keepalives

Help or Feedback

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

Simple Examples

Server Example

using SimpleTcp;

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

	// 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, ClientConnectedEventArgs e)
{
    Console.WriteLine("[" + e.IpPort + "] client connected");
}

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

static void DataReceived(object sender, DataReceivedFromClientEventArgs e)
{
    Console.WriteLine("[" + e.IpPort + "]: " + Encoding.UTF8.GetString(e.Data));
}

Client Example

using SimpleTcp;

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

	// 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, EventArgs e)
{
    Console.WriteLine("*** Server connected");
}

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

static void DataReceived(object sender, DataReceivedFromServerEventArgs e)
{
    Console.WriteLine("[" + _ServerIp + ":" + _ServerPort + "] " + Encoding.UTF8.GetString(e.Data));
}

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

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 SimpleTcp 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 enabled by default.

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 generally only work on server (being investigated)
  • Keepalive.TcpKeepAliveRetryCount is only applicable to .NET Core; for .NET Framework, this value is forced to 10

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.

simpletcp's People

Contributors

jchristn avatar tinohager avatar cmeeren avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.