From 295e3c10bb6d1c83796554edf8369e5aa87a20a8 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 9 Oct 2017 08:56:25 +0200 Subject: Initial commit. --- Server/ClientConnection.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Server/ClientConnection.cs (limited to 'Server/ClientConnection.cs') diff --git a/Server/ClientConnection.cs b/Server/ClientConnection.cs new file mode 100644 index 0000000..f2d8976 --- /dev/null +++ b/Server/ClientConnection.cs @@ -0,0 +1,34 @@ +using System; +using System.IO; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace Server +{ + public class ClientConnection + { + public StreamReader StreamReader { get; set; } + public StreamWriter StreamWriter { get; set; } + + public ClientConnection(TcpListener listener) + { + TcpClient connectionSocket = listener.AcceptTcpClient(); + NetworkStream ns = connectionSocket.GetStream(); + StreamReader = new StreamReader(ns); + StreamWriter = new StreamWriter(ns); + StreamWriter.AutoFlush = true; + Task.Run(() => EchoService(StreamReader, StreamWriter)); + } + + private async Task EchoService(StreamReader sr, StreamWriter sw) + { + while (true) + { + string message = await sr.ReadLineAsync(); + LogSingleton.Instance.Log += $"{message}\n"; + foreach (ClientConnection client in LogSingleton.Instance.ConnectedClients) client.StreamWriter.WriteLine(message); + message = ""; + } + } + } +} -- cgit v1.2.3