diff options
author | marcinzelent <zelent.marcin@protonmail.com> | 2017-10-09 08:56:25 +0200 |
---|---|---|
committer | marcinzelent <zelent.marcin@protonmail.com> | 2017-10-09 08:56:25 +0200 |
commit | 295e3c10bb6d1c83796554edf8369e5aa87a20a8 (patch) | |
tree | f6ad54798c039b6db9aceca3099fd409f6ea4ba8 /Server/ClientConnection.cs |
Initial commit.
Diffstat (limited to 'Server/ClientConnection.cs')
-rw-r--r-- | Server/ClientConnection.cs | 34 |
1 files changed, 34 insertions, 0 deletions
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 = ""; + } + } + } +} |