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/TcpChatServer.cs |
Initial commit.
Diffstat (limited to 'Server/TcpChatServer.cs')
-rw-r--r-- | Server/TcpChatServer.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Server/TcpChatServer.cs b/Server/TcpChatServer.cs new file mode 100644 index 0000000..986bbf1 --- /dev/null +++ b/Server/TcpChatServer.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace Server +{ + public class TcpChatServer + { + TcpListener serverSocket; + + public void Start(string ipAddress, int port) + { + try + { + serverSocket = new TcpListener(IPAddress.Parse(ipAddress), port); + serverSocket.Start(); + LogSingleton.Instance.Log += "Server started.\n"; + Task.Run(() => Listen(serverSocket)); + LogSingleton.Instance.Log += "Waiting for connections...\n"; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + + private void Listen(TcpListener listener) + { + while (true) + { + ClientConnection cc = new ClientConnection(listener); + LogSingleton.Instance.ConnectedClients.Add(cc); + } + } + + public void Stop() + { + serverSocket.Stop(); + LogSingleton.Instance.Log += "Server stopped\n"; + } + + public void SendMessage(string message) + { + foreach (ClientConnection client in LogSingleton.Instance.ConnectedClients) client.StreamWriter.WriteLine(message); + } + } +} |