summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Server/TcpChatServer.cs')
-rw-r--r--Server/TcpChatServer.cs50
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);
+ }
+ }
+}