static void Main() { Server server = new Server(8888); server.OnNewConnection += async(AbstractConnection client) => { client = new RsaDecorator(client); client.Connect(); Console.WriteLine("New connection!"); try { while (true) { Task <byte[]> task = client.GetMessageAsync(); await task; Console.WriteLine($"Has been received message: {Encoding.UTF8.GetString(task.Result)}"); client.Send(task.Result); } } catch { Console.WriteLine($"Connection has been closed"); } }; Task.Run(server.Listen); Console.ReadLine(); }
static void ByMyCode() { TCPServer server = new TCPServer(9999); server.OnNewConnection += async(connection) => { AbstractConnection rsaConnection = new RsaDecorator(connection); rsaConnection.Connect(); byte[] message = await rsaConnection.GetMessageAsync(); Console.WriteLine($"Receive message: {Encoding.UTF8.GetString(message)}"); }; server.Listen(); AbstractConnection connection = new RsaDecorator(new TcpConnection()); connection.SetEndPoint(IPAddress.Loopback.ToString(), 9999); string message = Console.ReadLine(); connection.Connect(); connection.Send(Encoding.UTF8.GetBytes(message)); Console.ReadLine(); }