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
Install / Use
/learn @chronoxor/NetCoreServerREADME
NetCoreServer
Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, Unix Domain Socket, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution.
Has integration with high-level message protocol based on Fast Binary Encoding
NetCoreServer documentation<br/> NetCoreServer downloads<br/>
Contents
- Features
- Requirements
- How to build?
- Examples
- Example: TCP chat server
- Example: TCP chat client
- Example: SSL chat server
- Example: SSL chat client
- Example: UDP echo server
- Example: UDP echo client
- Example: UDP multicast server
- Example: UDP multicast client
- Example: Unix Domain Socket chat server
- Example: Unix Domain Socket chat client
- Example: Simple protocol
- Example: Simple protocol server
- Example: Simple protocol client
- Example: HTTP server
- Example: HTTP client
- Example: HTTPS server
- Example: HTTPS client
- Example: WebSocket chat server
- Example: WebSocket chat client
- Example: WebSocket secure chat server
- Example: WebSocket secure chat client
- Performance
- OpenSSL certificates
Features
- Cross platform (Linux, MacOS, Windows)
- Asynchronous communication
- Supported transport protocols: TCP, SSL, UDP, UDP multicast, Unix Domain Socket
- Supported Web protocols: HTTP, HTTPS, WebSocket, WebSocket secure
- Supported Swagger OpenAPI iterative documentation
- Supported message protocol based on Fast Binary Encoding
Requirements
- Linux
- MacOS
- Windows
- .NET 6.0
- 7-Zip
- cmake
- git
- Visual Studio
Optional:
How to build?
Setup repository
git clone https://github.com/chronoxor/NetCoreServer.git
cd NetCoreServer
Linux
cd build
./unix.sh
MacOS
cd build
./unix.sh
Windows (Visual Studio)
Open and build NetCoreServer.sln or run the build script:
cd build
vs.bat
The build script will create "release" directory with zip files:
- NetCoreServer.zip - C# Server assembly
- Benchmarks.zip - C# Server benchmarks
- Examples.zip - C# Server examples
Examples
Example: TCP chat server
Here comes the example of the TCP chat server. It handles multiple TCP client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;
namespace TcpChatServer
{
class ChatSession : TcpSession
{
public ChatSession(TcpServer server) : base(server) {}
protected override void OnConnected()
{
Console.WriteLine($"Chat TCP session with Id {Id} connected!");
// Send invite message
string message = "Hello from TCP chat! Please send a message or '!' to disconnect the client!";
SendAsync(message);
}
protected override void OnDisconnected()
{
Console.WriteLine($"Chat TCP session with Id {Id} disconnected!");
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
Console.WriteLine("Incoming: " + message);
// Multicast message to all connected sessions
Server.Multicast(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Disconnect();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat TCP session caught an error with code {error}");
}
}
class ChatServer : TcpServer
{
public ChatServer(IPAddress address, int port) : base(address, port) {}
protected override TcpSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat TCP server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// TCP server port
int port = 1111;
if (args.Length > 0)
port = int.Parse(args[0]);
Console.WriteLine($"TCP server port: {port}");
Console.WriteLine();
// Create a new TCP chat server
var server = new ChatServer(IPAddress.Any, port);
// Start the server
Console.Write("Server starting...");
server.Start();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Restart the server
if (line == "!")
{
Console.Write("Server restarting...");
server.Restart();
Console.WriteLine("Done!");
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server.Multicast(line);
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}
Example: TCP chat client
Here comes the example of the TCP chat client. It connects to the TCP chat server and allows to send message to it and receive new messages.
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using TcpClient = NetCoreServer.TcpClient;
namespace TcpChatClient
{
class ChatClient : TcpClient
{
public ChatClient(string address, int port) : base(address, port) {}
