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.Tls13, 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!"); } } }