All Projects → jchristn → Simpletcp

jchristn / Simpletcp

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

Projects that are alternatives of or similar to Simpletcp

SuperSimpleTcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 263 (+165.66%)
Mutual labels:  ssl, tcp, messaging, mono, rpc, tcp-server, tcp-client
Watsontcp
WatsonTcp is the easiest way to build TCP-based clients and servers in C#.
Stars: ✭ 209 (+111.11%)
Mutual labels:  rpc, messaging, server, tcp, mono, ssl, client
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+2282.83%)
Mutual labels:  server, tcp, tcp-server, tcp-client, client
Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (+65.66%)
Mutual labels:  server, tcp, tcp-server, tcp-client, client
Hprose Nodejs
Hprose is a cross-language RPC. This project is Hprose 2.0 for Node.js
Stars: ✭ 297 (+200%)
Mutual labels:  rpc, tcp, tcp-server, tcp-client
React Native Tcp Socket
React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support
Stars: ✭ 112 (+13.13%)
Mutual labels:  tcp, tcp-server, tcp-client, ssl
Bizsocket
异步socket,对一些业务场景做了支持
Stars: ✭ 469 (+373.74%)
Mutual labels:  tcp, tcp-server, tcp-client, client
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-85.86%)
Mutual labels:  server, tcp, tcp-server, tcp-client
Jstp
Fast RPC for browser and Node.js based on TCP, WebSocket, and MDSF
Stars: ✭ 132 (+33.33%)
Mutual labels:  rpc, server, tcp, client
Easytcp
Simple framework for TCP clients and servers. Focused on performance and usability.
Stars: ✭ 60 (-39.39%)
Mutual labels:  tcp, tcp-server, tcp-client, ssl
Packetsender
Network utility for sending / receiving TCP, UDP, SSL
Stars: ✭ 1,349 (+1262.63%)
Mutual labels:  tcp, tcp-server, tcp-client, ssl
Cellnet
High performance, simple, extensible golang open source network library
Stars: ✭ 3,714 (+3651.52%)
Mutual labels:  rpc, server, tcp-server
EasyFileTransfer
An easy way to transfer file with any size on network with tcp protocol.
Stars: ✭ 30 (-69.7%)
Mutual labels:  tcp, tcp-server, tcp-client
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-32.32%)
Mutual labels:  tcp, tcp-server, tcp-client
Happypandax
A cross-platform server and client application for managing and reading manga and doujinshi
Stars: ✭ 432 (+336.36%)
Mutual labels:  rpc, server, client
Tacopie
C++ TCP Library - NO LONGER MAINTAINED
Stars: ✭ 359 (+262.63%)
Mutual labels:  tcp, tcp-server, tcp-client
Networksocket
NetworkSocket是一个以中间件(middleware)扩展通讯协议,以插件(plug)扩展服务器功能的支持SSL安全传输的通讯框架;目前支持http、websocket、fast、flex策略与silverlight策略协议。
Stars: ✭ 435 (+339.39%)
Mutual labels:  server, tcp, client
twjitm-core
采用Netty信息加载实现长连接实时通讯系统,客户端可以值任何场景,支持实时http通讯、webSocket通讯、tcp协议通讯、和udp协议通讯、广播协议等 通过http协议,rpc协议。 采用自定义网络数据包结构, 实现自定义网络栈。
Stars: ✭ 98 (-1.01%)
Mutual labels:  tcp, rpc, tcp-server
Cppserver
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Stars: ✭ 528 (+433.33%)
Mutual labels:  tcp-server, tcp-client, ssl
Netcoreserver
Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Stars: ✭ 799 (+707.07%)
Mutual labels:  tcp-server, tcp-client, ssl

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.

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 v2.4.0

  • Breaking change, timeouts now use milliseconds instead of seconds (does not apply to keepalive settings)

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");

  // 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, DataReceivedEventArgs 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");

  // 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, DataReceivedEventArgs e)
{
  Console.WriteLine("[" + e.IpPort + "] " + Encoding.UTF8.GetString(e.Data));
}

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(10); // try for up to 10 seconds

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

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 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

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

Special Thanks

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

@u1035 @cmeeren @tinohager @pha3z @opnop @kopkarmecoindo @simonhaines @matt1tk @lukeacat @exergist @maynardsi

Version History

Please refer to CHANGELOG.md.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].