| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
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.
NetCoreServer documentation
NetCoreServer downloads
Optional:
git clone https://github.com/chronoxor/NetCoreServer.git
cd NetCoreServercd build
./unix.shcd build
./unix.shOpen and build NetCoreServer.sln or run the build script:
cd build
vs.batThe build script will create "release" directory with zip files:
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!");
}
}
}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) {}
public void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected)
Thread.Yield();
}
protected override void OnConnected()
{
Console.WriteLine($"Chat TCP client connected a new session with Id {Id}");
}
protected override void OnDisconnected()
{
Console.WriteLine($"Chat TCP client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
Console.WriteLine(Encoding.UTF8.GetString(buffer, (int)offset, (int)size));
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat TCP client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// TCP server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// TCP server port
int port = 1111;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"TCP server address: {address}");
Console.WriteLine($"TCP server port: {port}");
Console.WriteLine();
// Create a new TCP chat client
var client = new ChatClient(address, port);
// Connect the client
Console.Write("Client connecting...");
client.ConnectAsync();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Disconnect the client
if (line == "!")
{
Console.Write("Client disconnecting...");
client.DisconnectAsync();
Console.WriteLine("Done!");
continue;
}
// Send the entered text to the chat server
client.SendAsync(line);
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the SSL chat server. It handles multiple SSL client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
This example is very similar to the TCP one except the code that prepares SSL context and handshake handler.
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using NetCoreServer;
namespace SslChatServer
{
class ChatSession : SslSession
{
public ChatSession(SslServer server) : base(server) {}
protected override void OnConnected()
{
Console.WriteLine($"Chat SSL session with Id {Id} connected!");
}
protected override void OnHandshaked()
{
Console.WriteLine($"Chat SSL session with Id {Id} handshaked!");
// Send invite message
string message = "Hello from SSL chat! Please send a message or '!' to disconnect the client!";
Send(message);
}
protected override void OnDisconnected()
{
Console.WriteLine($"Chat SSL 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 SSL session caught an error with code {error}");
}
}
class ChatServer : SslServer
{
public ChatServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
protected override SslSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat SSL server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// SSL server port
int port = 2222;
if (args.Length > 0)
port = int.Parse(args[0]);
Console.WriteLine($"SSL server port: {port}");
Console.WriteLine();
// Create and prepare a new SSL server context
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("server.pfx", "qwerty"));
// Create a new SSL chat server
var server = new ChatServer(context, 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!");
}
}
}Here comes the example of the SSL chat client. It connects to the SSL chat server and allows to send message to it and receive new messages.
This example is very similar to the TCP one except the code that prepares SSL context and handshake handler.
using System;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using NetCoreServer;
namespace SslChatClient
{
class ChatClient : SslClient
{
public ChatClient(SslContext context, string address, int port) : base(context, address, port) {}
public void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected)
Thread.Yield();
}
protected override void OnConnected()
{
Console.WriteLine($"Chat SSL client connected a new session with Id {Id}");
}
protected override void OnHandshaked()
{
Console.WriteLine($"Chat SSL client handshaked a new session with Id {Id}");
}
protected override void OnDisconnected()
{
Console.WriteLine($"Chat SSL client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
Console.WriteLine(Encoding.UTF8.GetString(buffer, (int)offset, (int)size));
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat SSL client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// SSL server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// SSL server port
int port = 2222;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"SSL server address: {address}");
Console.WriteLine($"SSL server port: {port}");
Console.WriteLine();
// Create and prepare a new SSL client context
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
// Create a new SSL chat client
var client = new ChatClient(context, address, port);
// Connect the client
Console.Write("Client connecting...");
client.ConnectAsync();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Disconnect the client
if (line == "!")
{
Console.Write("Client disconnecting...");
client.DisconnectAsync();
Console.WriteLine("Done!");
continue;
}
// Send the entered text to the chat server
client.SendAsync(line);
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the UDP echo server. It receives a datagram mesage from any UDP client and resend it back without any changes.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;
namespace UdpEchoServer
{
class EchoServer : UdpServer
{
public EchoServer(IPAddress address, int port) : base(address, port) {}
protected override void OnStarted()
{
// Start receive datagrams
ReceiveAsync();
}
protected override void OnReceived(EndPoint endpoint, byte[] buffer, long offset, long size)
{
Console.WriteLine("Incoming: " + Encoding.UTF8.GetString(buffer, (int)offset, (int)size));
// Echo the message back to the sender
SendAsync(endpoint, buffer, 0, size);
}
protected override void OnSent(EndPoint endpoint, long sent)
{
// Continue receive datagrams
ReceiveAsync();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Echo UDP server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// UDP server port
int port = 3333;
if (args.Length > 0)
port = int.Parse(args[0]);
Console.WriteLine($"UDP server port: {port}");
Console.WriteLine();
// Create a new UDP echo server
var server = new EchoServer(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!");
}
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the UDP echo client. It sends user datagram message to UDP server and listen for response.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UdpClient = NetCoreServer.UdpClient;
namespace UdpEchoClient
{
class EchoClient : UdpClient
{
public EchoClient(string address, int port) : base(address, port) {}
public void DisconnectAndStop()
{
_stop = true;
Disconnect();
while (IsConnected)
Thread.Yield();
}
protected override void OnConnected()
{
Console.WriteLine($"Echo UDP client connected a new session with Id {Id}");
// Start receive datagrams
ReceiveAsync();
}
protected override void OnDisconnected()
{
Console.WriteLine($"Echo UDP client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
Connect();
}
protected override void OnReceived(EndPoint endpoint, byte[] buffer, long offset, long size)
{
Console.WriteLine("Incoming: " + Encoding.UTF8.GetString(buffer, (int)offset, (int)size));
// Continue receive datagrams
ReceiveAsync();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Echo UDP client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// UDP server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// UDP server port
int port = 3333;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"UDP server address: {address}");
Console.WriteLine($"UDP server port: {port}");
Console.WriteLine();
// Create a new TCP chat client
var client = new EchoClient(address, port);
// Connect the client
Console.Write("Client connecting...");
client.Connect();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Disconnect the client
if (line == "!")
{
Console.Write("Client disconnecting...");
client.Disconnect();
Console.WriteLine("Done!");
continue;
}
// Send the entered text to the chat server
client.Send(line);
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the UDP multicast server. It use multicast IP address to multicast datagram messages to all client that joined corresponding UDP multicast group.
using System;
using System.Net;
using System.Net.Sockets;
using NetCoreServer;
namespace UdpMulticastServer
{
class MulticastServer : UdpServer
{
public MulticastServer(IPAddress address, int port) : base(address, port) {}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Multicast UDP server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// UDP multicast address
string multicastAddress = "239.255.0.1";
if (args.Length > 0)
multicastAddress = args[0];
// UDP multicast port
int multicastPort = 3334;
if (args.Length > 1)
multicastPort = int.Parse(args[1]);
Console.WriteLine($"UDP multicast address: {multicastAddress}");
Console.WriteLine($"UDP multicast port: {multicastPort}");
Console.WriteLine();
// Create a new UDP multicast server
var server = new MulticastServer(IPAddress.Any, 0);
// Start the multicast server
Console.Write("Server starting...");
server.Start(multicastAddress, multicastPort);
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!");
}
}
}Here comes the example of the UDP multicast client. It use multicast IP address and joins UDP multicast group in order to receive multicasted datagram messages from UDP server.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UdpClient = NetCoreServer.UdpClient;
namespace UdpMulticastClient
{
class MulticastClient : UdpClient
{
public string Multicast;
public MulticastClient(string address, int port) : base(address, port) {}
public void DisconnectAndStop()
{
_stop = true;
Disconnect();
while (IsConnected)
Thread.Yield();
}
protected override void OnConnected()
{
Console.WriteLine($"Multicast UDP client connected a new session with Id {Id}");
// Join UDP multicast group
JoinMulticastGroup(Multicast);
// Start receive datagrams
ReceiveAsync();
}
protected override void OnDisconnected()
{
Console.WriteLine($"Multicast UDP client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
Connect();
}
protected override void OnReceived(EndPoint endpoint, byte[] buffer, long offset, long size)
{
Console.WriteLine("Incoming: " + Encoding.UTF8.GetString(buffer, (int)offset, (int)size));
// Continue receive datagrams
ReceiveAsync();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Multicast UDP client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// UDP listen address
string listenAddress = "0.0.0.0";
if (args.Length > 0)
listenAddress = args[0];
// UDP multicast address
string multicastAddress = "239.255.0.1";
if (args.Length > 1)
multicastAddress = args[1];
// UDP multicast port
int multicastPort = 3334;
if (args.Length > 2)
multicastPort = int.Parse(args[2]);
Console.WriteLine($"UDP listen address: {listenAddress}");
Console.WriteLine($"UDP multicast address: {multicastAddress}");
Console.WriteLine($"UDP multicast port: {multicastPort}");
Console.WriteLine();
// Create a new TCP chat client
var client = new MulticastClient(listenAddress, multicastPort);
client.SetupMulticast(true);
client.Multicast = multicastAddress;
// Connect the client
Console.Write("Client connecting...");
client.Connect();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Disconnect the client
if (line == "!")
{
Console.Write("Client disconnecting...");
client.Disconnect();
Console.WriteLine("Done!");
continue;
}
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the HTTP cache server. It allows to manipulate cache data with HTTP methods (GET, POST, PUT and DELETE).
Use the following link to open Swagger OpenAPI iterative documentation: http://localhost:8080/api/index.html
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using NetCoreServer;
namespace HttpServer
{
class CommonCache
{
public static CommonCache GetInstance()
{
if (_instance == null)
_instance = new CommonCache();
return _instance;
}
public bool GetCache(string key, out string value)
{
return _cache.TryGetValue(key, out value);
}
public void SetCache(string key, string value)
{
_cache[key] = value;
}
public bool DeleteCache(string key, out string value)
{
return _cache.TryRemove(key, out value);
}
private readonly ConcurrentDictionary<string, string> _cache = new ConcurrentDictionary<string, string>();
private static CommonCache _instance;
}
class HttpCacheSession : HttpSession
{
public HttpCacheSession(NetCoreServer.HttpServer server) : base(server) {}
protected override void OnReceivedRequest(HttpRequest request)
{
// Show HTTP request content
Console.WriteLine(request);
// Process HTTP request methods
if (request.Method == "HEAD")
SendResponseAsync(Response.MakeHeadResponse());
else if (request.Method == "GET")
{
// Get the cache value
string cache;
if (CommonCache.GetInstance().GetCache(request.Url, out cache))
{
// Response with the cache value
SendResponseAsync(Response.MakeGetResponse(cache));
}
else
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + request.Url));
}
else if ((request.Method == "POST") || (request.Method == "PUT"))
{
// Set the cache value
CommonCache.GetInstance().SetCache(request.Url, request.Body);
// Response with the cache value
SendResponseAsync(Response.MakeOkResponse());
}
else if (request.Method == "DELETE")
{
// Delete the cache value
string cache;
if (CommonCache.GetInstance().DeleteCache(request.Url, out cache))
{
// Response with the cache value
SendResponseAsync(Response.MakeGetResponse(cache));
}
else
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + request.Url));
}
else if (request.Method == "OPTIONS")
SendResponseAsync(Response.MakeOptionsResponse());
else if (request.Method == "TRACE")
SendResponseAsync(Response.MakeTraceResponse(request.Cache.Data));
else
SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
}
protected override void OnReceivedRequestError(HttpRequest request, string error)
{
Console.WriteLine($"Request error: {error}");
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"HTTP session caught an error: {error}");
}
}
class HttpCacheServer : NetCoreServer.HttpServer
{
public HttpCacheServer(IPAddress address, int port) : base(address, port) {}
protected override TcpSession CreateSession() { return new HttpCacheSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"HTTP session caught an error: {error}");
}
}
class Program
{
static void Main(string[] args)
{
// HTTP server port
int port = 8080;
if (args.Length > 0)
port = int.Parse(args[0]);
// HTTP server content path
string www = "../../../../../www/api";
if (args.Length > 1)
www = args[1];
Console.WriteLine($"HTTP server port: {port}");
Console.WriteLine($"HTTP server static content path: {www}");
Console.WriteLine($"HTTP server website: http://localhost:{port}/api/index.html");
Console.WriteLine();
// Create a new HTTP server
var server = new HttpCacheServer(IPAddress.Any, port);
server.AddStaticContent(www, "/api");
// 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!");
}
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the HTTP client. It allows to send HTTP requests (GET, POST, PUT and DELETE) and receive HTTP responses.
using System;
using NetCoreServer;
namespace HttpClient
{
class Program
{
static void Main(string[] args)
{
// HTTP server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// HTTP server port
int port = 8080;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"HTTP server address: {address}");
Console.WriteLine($"HTTP server port: {port}");
Console.WriteLine();
// Create a new HTTP client
var client = new HttpClientEx(address, port);
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Reconnect the client
if (line == "!")
{
Console.Write("Client reconnecting...");
if (client.IsConnected)
client.ReconnectAsync();
else
client.ConnectAsync();
Console.WriteLine("Done!");
continue;
}
var commands = line.Split(' ');
if (commands.Length < 2)
{
Console.WriteLine("HTTP method and URL must be entered!");
continue;
}
if (commands[0].ToUpper() == "HEAD")
{
var response = client.SendHeadRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "GET")
{
var response = client.SendGetRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "POST")
{
if (commands.Length < 3)
{
Console.WriteLine("HTTP method, URL and body must be entered!");
continue;
}
var response = client.SendPostRequest(commands[1], commands[2]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "PUT")
{
if (commands.Length < 3)
{
Console.WriteLine("HTTP method, URL and body must be entered!");
continue;
}
var response = client.SendPutRequest(commands[1], commands[2]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "DELETE")
{
var response = client.SendDeleteRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "OPTIONS")
{
var response = client.SendOptionsRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "TRACE")
{
var response = client.SendTraceRequest(commands[1]).Result;
Console.WriteLine(response);
}
else
Console.WriteLine("Unknown HTTP method");
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.Disconnect();
Console.WriteLine("Done!");
}
}
}Here comes the example of the HTTPS cache server. It allows to manipulate cache data with HTTP methods (GET, POST, PUT and DELETE) with secured transport protocol.
Use the following link to open Swagger OpenAPI iterative documentation: https://localhost:8443/api/index.html
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using NetCoreServer;
namespace HttpsServer
{
class CommonCache
{
public static CommonCache GetInstance()
{
if (_instance == null)
_instance = new CommonCache();
return _instance;
}
public bool GetCache(string key, out string value)
{
return _cache.TryGetValue(key, out value);
}
public void SetCache(string key, string value)
{
_cache[key] = value;
}
public bool DeleteCache(string key, out string value)
{
return _cache.TryRemove(key, out value);
}
private readonly ConcurrentDictionary<string, string> _cache = new ConcurrentDictionary<string, string>();
private static CommonCache _instance;
}
class HttpsCacheSession : HttpsSession
{
public HttpsCacheSession(NetCoreServer.HttpsServer server) : base(server) { }
protected override void OnReceivedRequest(HttpRequest request)
{
// Show HTTP request content
Console.WriteLine(request);
// Process HTTP request methods
if (request.Method == "HEAD")
SendResponseAsync(Response.MakeHeadResponse());
else if (request.Method == "GET")
{
// Get the cache value
string cache;
if (CommonCache.GetInstance().GetCache(request.Url, out cache))
{
// Response with the cache value
SendResponseAsync(Response.MakeGetResponse(cache));
}
else
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + request.Url));
}
else if ((request.Method == "POST") || (request.Method == "PUT"))
{
// Set the cache value
CommonCache.GetInstance().SetCache(request.Url, request.Body);
// Response with the cache value
SendResponseAsync(Response.MakeOkResponse());
}
else if (request.Method == "DELETE")
{
// Delete the cache value
string cache;
if (CommonCache.GetInstance().DeleteCache(request.Url, out cache))
{
// Response with the cache value
SendResponseAsync(Response.MakeGetResponse(cache));
}
else
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + request.Url));
}
else if (request.Method == "OPTIONS")
SendResponseAsync(Response.MakeOptionsResponse());
else if (request.Method == "TRACE")
SendResponseAsync(Response.MakeTraceResponse(request.Cache));
else
SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
}
protected override void OnReceivedRequestError(HttpRequest request, string error)
{
Console.WriteLine($"Request error: {error}");
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"HTTPS session caught an error: {error}");
}
}
class HttpsCacheServer : NetCoreServer.HttpsServer
{
public HttpsCacheServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
protected override SslSession CreateSession() { return new HttpsCacheSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"HTTPS server caught an error: {error}");
}
}
class Program
{
static void Main(string[] args)
{
// HTTPS server port
int port = 8443;
if (args.Length > 0)
port = int.Parse(args[0]);
// HTTPS server content path
string www = "../../../../../www/api";
if (args.Length > 1)
www = args[1];
Console.WriteLine($"HTTPS server port: {port}");
Console.WriteLine($"HTTPS server static content path: {www}");
Console.WriteLine($"HTTPS server website: https://localhost:{port}/api/index.html");
Console.WriteLine();
// Create and prepare a new SSL server context
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("server.pfx", "qwerty"));
// Create a new HTTP server
var server = new HttpsCacheServer(context, IPAddress.Any, port);
server.AddStaticContent(www, "/api");
// 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!");
}
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the HTTPS client. It allows to send HTTP requests (GET, POST, PUT and DELETE) and receive HTTP responses with secured transport protocol.
using System;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using NetCoreServer;
namespace HttpsClient
{
class Program
{
static void Main(string[] args)
{
// HTTPS server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// HTTPS server port
int port = 8443;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"HTTPS server address: {address}");
Console.WriteLine($"HTTPS server port: {port}");
Console.WriteLine();
// Create and prepare a new SSL client context
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
// Create a new HTTPS client
var client = new HttpsClientEx(context, address, port);
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Reconnect the client
if (line == "!")
{
Console.Write("Client reconnecting...");
if (client.IsConnected)
client.ReconnectAsync();
else
client.ConnectAsync();
Console.WriteLine("Done!");
continue;
}
var commands = line.Split(' ');
if (commands.Length < 2)
{
Console.WriteLine("HTTP method and URL must be entered!");
continue;
}
if (commands[0].ToUpper() == "HEAD")
{
var response = client.SendHeadRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "GET")
{
var response = client.SendGetRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "POST")
{
if (commands.Length < 3)
{
Console.WriteLine("HTTP method, URL and body must be entered!");
continue;
}
var response = client.SendPostRequest(commands[1], commands[2]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "PUT")
{
if (commands.Length < 3)
{
Console.WriteLine("HTTP method, URL and body must be entered!");
continue;
}
var response = client.SendPutRequest(commands[1], commands[2]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "DELETE")
{
var response = client.SendDeleteRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "OPTIONS")
{
var response = client.SendOptionsRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "TRACE")
{
var response = client.SendTraceRequest(commands[1]).Result;
Console.WriteLine(response);
}
else
Console.WriteLine("Unknown HTTP method");
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.Disconnect();
Console.WriteLine("Done!");
}
}
}Here comes the example of the WebSocket chat server. It handles multiple WebSocket client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
Use the following link to open WebSocket chat server example: http://localhost:8080/chat/index.html
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;
namespace WsChatServer
{
class ChatSession : WsSession
{
public ChatSession(WsServer server) : base(server) { }
public override void OnWsConnected(HttpRequest request)
{
Console.WriteLine($"Chat WebSocket session with Id {Id} connected!");
// Send invite message
string message = "Hello from WebSocket chat! Please send a message or '!' to disconnect the client!";
SendTextAsync(message);
}
public override void OnWsDisconnected()
{
Console.WriteLine($"Chat WebSocket session with Id {Id} disconnected!");
}
public override void OnWsReceived(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
((WsServer)Server).MulticastText(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close(1000);
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket session caught an error with code {error}");
}
}
class ChatServer : WsServer
{
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 WebSocket server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// WebSocket server port
int port = 8080;
if (args.Length > 0)
port = int.Parse(args[0]);
// WebSocket server content path
string www = "../../../../../www/ws";
if (args.Length > 1)
www = args[1];
Console.WriteLine($"WebSocket server port: {port}");
Console.WriteLine($"WebSocket server static content path: {www}");
Console.WriteLine($"WebSocket server website: http://localhost:{port}/chat/index.html");
Console.WriteLine();
// Create a new WebSocket server
var server = new ChatServer(IPAddress.Any, port);
server.AddStaticContent(www, "/chat");
// 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!");
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server.MulticastText(line);
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the WebSocket chat client. It connects to the WebSocket 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 NetCoreServer;
namespace WsChatClient
{
class ChatClient : WsClient
{
public ChatClient(string address, int port) : base(address, port) { }
public void DisconnectAndStop()
{
_stop = true;
CloseAsync(1000);
while (IsConnected)
Thread.Yield();
}
public override void OnWsConnecting(HttpRequest request)
{
request.SetBegin("GET", "/");
request.SetHeader("Host", "localhost");
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
public override void OnWsConnected(HttpResponse response)
{
Console.WriteLine($"Chat WebSocket client connected a new session with Id {Id}");
}
public override void OnWsDisconnected()
{
Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}");
}
public override void OnWsReceived(byte[] buffer, long offset, long size)
{
Console.WriteLine($"Incoming: {Encoding.UTF8.GetString(buffer, (int)offset, (int)size)}");
}
protected override void OnDisconnected()
{
base.OnDisconnected();
Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// WebSocket server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// WebSocket server port
int port = 8080;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"WebSocket server address: {address}");
Console.WriteLine($"WebSocket server port: {port}");
Console.WriteLine();
// Create a new TCP chat client
var client = new ChatClient(address, port);
// Connect the client
Console.Write("Client connecting...");
client.ConnectAsync();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Disconnect the client
if (line == "!")
{
Console.Write("Client disconnecting...");
client.DisconnectAsync();
Console.WriteLine("Done!");
continue;
}
// Send the entered text to the chat server
client.SendTextAsync(line);
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the WebSocket secure chat server. It handles multiple WebSocket secure client sessions and multicast received message from any session to all ones. Also it is possible to send admin message directly from the server.
This example is very similar to the WebSocket one except the code that prepares WebSocket secure context and handshake handler.
Use the following link to open WebSocket secure chat server example: https://localhost:8443/chat/index.html
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using NetCoreServer;
namespace WssChatServer
{
class ChatSession : WssSession
{
public ChatSession(WssServer server) : base(server) { }
public override void OnWsConnected(HttpRequest request)
{
Console.WriteLine($"Chat WebSocket session with Id {Id} connected!");
// Send invite message
string message = "Hello from WebSocket chat! Please send a message or '!' to disconnect the client!";
SendTextAsync(message);
}
public override void OnWsDisconnected()
{
Console.WriteLine($"Chat WebSocket session with Id {Id} disconnected!");
}
public override void OnWsReceived(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
((WssServer)Server).MulticastText(message);
// If the buffer starts with '!' the disconnect the current session
if (message == "!")
Close(1000);
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket session caught an error with code {error}");
}
}
class ChatServer : WssServer
{
public ChatServer(SslContext context, IPAddress address, int port) : base(context, address, port) { }
protected override SslSession CreateSession() { return new ChatSession(this); }
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// WebSocket server port
int port = 8443;
if (args.Length > 0)
port = int.Parse(args[0]);
// WebSocket server content path
string www = "../../../../../www/wss";
if (args.Length > 1)
www = args[1];
Console.WriteLine($"WebSocket server port: {port}");
Console.WriteLine($"WebSocket server static content path: {www}");
Console.WriteLine($"WebSocket server website: https://localhost:{port}/chat/index.html");
Console.WriteLine();
// Create and prepare a new SSL server context
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("server.pfx", "qwerty"));
// Create a new WebSocket server
var server = new ChatServer(context, IPAddress.Any, port);
server.AddStaticContent(www, "/chat");
// 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!");
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server.MulticastText(line);
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}Here comes the example of the WebSocket secure chat client. It connects to the WebSocket secure chat server and allows to send message to it and receive new messages.
This example is very similar to the WebSocket one except the code that prepares WebSocket secure context and handshake handler.
using System;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using NetCoreServer;
namespace WssChatClient
{
class ChatClient : WssClient
{
public ChatClient(SslContext context, string address, int port) : base(context, address, port) { }
public void DisconnectAndStop()
{
_stop = true;
CloseAsync(1000);
while (IsConnected)
Thread.Yield();
}
public override void OnWsConnecting(HttpRequest request)
{
request.SetBegin("GET", "/");
request.SetHeader("Host", "localhost");
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(Encoding.UTF8.GetBytes(Id.ToString())));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
}
public override void OnWsConnected(HttpResponse response)
{
Console.WriteLine($"Chat WebSocket client connected a new session with Id {Id}");
}
public override void OnWsDisconnected()
{
Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}");
}
public override void OnWsReceived(byte[] buffer, long offset, long size)
{
Console.WriteLine($"Incoming: {Encoding.UTF8.GetString(buffer, (int)offset, (int)size)}");
}
protected override void OnDisconnected()
{
base.OnDisconnected();
Console.WriteLine($"Chat WebSocket client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat WebSocket client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// WebSocket server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// WebSocket server port
int port = 8443;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"WebSocket server address: {address}");
Console.WriteLine($"WebSocket server port: {port}");
Console.WriteLine();
// Create and prepare a new SSL client context
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
// Create a new TCP chat client
var client = new ChatClient(context, address, port);
// Connect the client
Console.Write("Client connecting...");
client.ConnectAsync();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Reconnect the client
if (line == "!")
{
Console.Write("Client reconnecting...");
if (client.IsConnected)
client.ReconnectAsync();
else
client.ConnectAsync();
Console.WriteLine("Done!");
continue;
}
// Send the entered text to the chat server
client.SendTextAsync(line);
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}Here comes several communication scenarios with timing measurements.
Benchmark environment is the following:
CPU architecutre: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz CPU logical cores: 8 CPU physical cores: 4 CPU clock speed: 3.998 GHz CPU Hyper-Threading: enabled RAM total: 31.962 GiB RAM free: 24.011 GiB OS version: Microsoft Windows 8 Enterprise Edition (build 9200), 64-bit OS bits: 64-bit Process bits: 64-bit Process configuaraion: release
This scenario sends lots of messages from several clients to a server. The server responses to each message and resend the similar response to the client. The benchmark measures total Round-trip time to send all messages and receive all responses, messages & data throughput, count of errors.
Server address: 127.0.0.1 Server port: 1111 Working clients: 1 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.023 s Total data: 92.1007 MiB Total messages: 3046895 Data throughput: 9.283 MiB/s Message latency: 3.289 mcs Message throughput: 303975 msg/s
Server address: 127.0.0.1 Server port: 1111 Working clients: 100 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 12.391 s Total data: 304.533 MiB Total messages: 9978530 Data throughput: 24.587 MiB/s Message latency: 1.241 mcs Message throughput: 805245 msg/s
Server address: 127.0.0.1 Server port: 2222 Working clients: 1 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.025 s Total data: 41.819 MiB Total messages: 1369713 Data throughput: 4.173 MiB/s Message latency: 7.319 mcs Message throughput: 136624 msg/s
Server address: 127.0.0.1 Server port: 2222 Working clients: 100 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.501 s Total data: 72.719 MiB Total messages: 2382316 Data throughput: 6.945 MiB/s Message latency: 4.407 mcs Message throughput: 226861 msg/s
Server address: 127.0.0.1 Server port: 3333 Working clients: 1 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.033 s Total data: 18.779 MiB Total messages: 614775 Data throughput: 1.890 MiB/s Message latency: 16.320 mcs Message throughput: 61273 msg/s
Server address: 127.0.0.1 Server port: 3333 Working clients: 100 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.637 s Total data: 20.1000 MiB Total messages: 687481 Data throughput: 1.995 MiB/s Message latency: 15.472 mcs Message throughput: 64630 msg/s
Server address: 127.0.0.1 Server port: 8080 Working clients: 1 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.025 s Total data: 43.799 MiB Total messages: 1434609 Data throughput: 4.375 MiB/s Message latency: 6.988 mcs Message throughput: 143101 msg/s
Server address: 127.0.0.1 Server port: 8080 Working clients: 100 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 14.625 s Total data: 47.533 MiB Total messages: 1557172 Data throughput: 3.255 MiB/s Message latency: 9.392 mcs Message throughput: 106471 msg/s
Server address: 127.0.0.1 Server port: 8443 Working clients: 1 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.029 s Total data: 36.274 MiB Total messages: 1188443 Data throughput: 3.631 MiB/s Message latency: 8.438 mcs Message throughput: 118497 msg/s
Server address: 127.0.0.1 Server port: 8443 Working clients: 100 Working messages: 1000 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.381 s Total data: 59.936 MiB Total messages: 1963276 Data throughput: 5.789 MiB/s Message latency: 5.288 mcs Message throughput: 189103 msg/s
In this scenario server multicasts messages to all connected clients. The benchmark counts total messages received by all clients for all the working time and measures messages & data throughput, count of errors.
Server address: 127.0.0.1 Server port: 1111 Working clients: 1 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.029 s Total data: 25.021 MiB Total messages: 819888 Data throughput: 2.506 MiB/s Message latency: 12.233 mcs Message throughput: 81744 msg/s
Server address: 127.0.0.1 Server port: 1111 Working clients: 100 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.045 s Total data: 22.980 MiB Total messages: 752279 Data throughput: 2.292 MiB/s Message latency: 13.353 mcs Message throughput: 74889 msg/s
Server address: 127.0.0.1 Server port: 2222 Working clients: 1 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.138 s Total data: 20.370 MiB Total messages: 667214 Data throughput: 2.008 MiB/s Message latency: 15.195 mcs Message throughput: 65809 msg/s
Server address: 127.0.0.1 Server port: 2222 Working clients: 100 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.417 s Total data: 19.600 MiB Total messages: 641823 Data throughput: 1.901 MiB/s Message latency: 16.230 mcs Message throughput: 61612 msg/s
Server address: 239.255.0.1 Server port: 3333 Working clients: 1 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.024 s Total data: 14.942 MiB Total messages: 488896 Data throughput: 1.500 MiB/s Message latency: 20.505 mcs Message throughput: 48768 msg/s
Server address: 239.255.0.1 Server port: 3333 Working clients: 100 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.040 s Total data: 27.1010 MiB Total messages: 917062 Data throughput: 2.806 MiB/s Message latency: 10.948 mcs Message throughput: 91333 msg/s
Server address: 127.0.0.1 Server port: 8080 Working clients: 1 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.034 s Total data: 53.338 MiB Total messages: 1747523 Data throughput: 5.322 MiB/s Message latency: 5.742 mcs Message throughput: 174145 msg/s
Server address: 127.0.0.1 Server port: 8080 Working clients: 100 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.083 s Total data: 23.541 MiB Total messages: 770984 Data throughput: 2.341 MiB/s Message latency: 13.078 mcs Message throughput: 76459 msg/s
Server address: 127.0.0.1 Server port: 8443 Working clients: 1 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.034 s Total data: 19.315 MiB Total messages: 632689 Data throughput: 1.946 MiB/s Message latency: 15.859 mcs Message throughput: 63052 msg/s
Server address: 127.0.0.1 Server port: 8443 Working clients: 100 Message size: 32 Seconds to benchmarking: 10 Errors: 0 Total time: 10.249 s Total data: 19.434 MiB Total messages: 636502 Data throughput: 1.916 MiB/s Message latency: 16.103 mcs Message throughput: 62098 msg/s
Server address: 127.0.0.1 Server port: 8080 Working clients: 1 Working messages: 1 Seconds to benchmarking: 10 Errors: 0 Total time: 10.023 s Total data: 13.1015 MiB Total messages: 138409 Data throughput: 1.405 MiB/s Message latency: 72.419 mcs Message throughput: 13808 msg/s
Server address: 127.0.0.1 Server port: 8080 Working clients: 100 Working messages: 1 Seconds to benchmarking: 10 Errors: 0 Total time: 12.068 s Total data: 47.220 MiB Total messages: 469069 Data throughput: 3.934 MiB/s Message latency: 25.728 mcs Message throughput: 38867 msg/s
Server address: 127.0.0.1 Server port: 8443 Working clients: 1 Working messages: 1 Seconds to benchmarking: 10 Errors: 0 Total time: 10.022 s Total data: 20.674 MiB Total messages: 204361 Data throughput: 2.062 MiB/s Message latency: 49.043 mcs Message throughput: 20390 msg/s
Server address: 127.0.0.1 Server port: 8443 Working clients: 100 Working messages: 1 Seconds to benchmarking: 10 Errors: 0 Total time: 10.786 s Total data: 91.020 MiB Total messages: 896107 Data throughput: 8.448 MiB/s Message latency: 12.037 mcs Message throughput: 83073 msg/s
In order to create OpenSSL based server and client you should prepare a set of SSL certificates. Here comes several steps to get a self-signed set of SSL certificates for testing purposes:
openssl genrsa -passout pass:qwerty -out ca-secret.key 4096openssl rsa -passin pass:qwerty -in ca-secret.key -out ca.keyopenssl req -new -x509 -days 3650 -subj '/C=BY/ST=Belarus/L=Minsk/O=Example root CA/OU=Example CA unit/CN=example.com' -key ca.key -out ca.crtopenssl pkcs12 -export -passout pass:qwerty -inkey ca.key -in ca.crt -out ca.pfxopenssl pkcs12 -passin pass:qwerty -passout pass:qwerty -in ca.pfx -out ca.pemopenssl genrsa -passout pass:qwerty -out server-secret.key 4096openssl rsa -passin pass:qwerty -in server-secret.key -out server.keyopenssl req -new -subj '/C=BY/ST=Belarus/L=Minsk/O=Example server/OU=Example server unit/CN=server.example.com' -key server.key -out server.csropenssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crtopenssl pkcs12 -export -passout pass:qwerty -inkey server.key -in server.crt -out server.pfxopenssl pkcs12 -passin pass:qwerty -passout pass:qwerty -in server.pfx -out server.pemopenssl genrsa -passout pass:qwerty -out client-secret.key 4096openssl rsa -passin pass:qwerty -in client-secret.key -out client.keyopenssl req -new -subj '/C=BY/ST=Belarus/L=Minsk/O=Example client/OU=Example client unit/CN=client.example.com' -key client.key -out client.csropenssl x509 -req -days 3650 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crtopenssl pkcs12 -export -passout pass:qwerty -inkey client.key -in client.crt -out client.pfxopenssl pkcs12 -passin pass:qwerty -passout pass:qwerty -in client.pfx -out client.pemopenssl dhparam -out dh4096.pem 4096| Back | FazBrowse Home | New Git URL |