From 6861889a8d439183b40d28b9f4c361b841c35b44 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 10 Apr 2016 20:12:14 -1000 Subject: [PATCH] Added code to prevent spawning of multiple TcpListeners on the same port. Also fixed a bug where the server was asking for a string for the message to be sent multiple times --- .../Server application/ChatServer.cs | 27 ++++++++++++------- .../Server application/MainServer.cs | 26 +++++++++++------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Server application/Server application/ChatServer.cs b/Server application/Server application/ChatServer.cs index 8a59635..1d3cf0d 100644 --- a/Server application/Server application/ChatServer.cs +++ b/Server application/Server application/ChatServer.cs @@ -14,6 +14,9 @@ namespace Server_application { class ChatServer : TCPServer { + protected static TcpListener listener = null; + protected static bool listenerStarted = false; + private static uint currentID = 0; public static readonly object IDLock = new object(); private Dictionary clientList = new Dictionary(); @@ -54,16 +57,21 @@ namespace Server_application public override void startServer() { //attempts to start listener on chat port - try + if (!listenerStarted) { - listener = new TcpListener(IPAddress.Any, port); - listener.Start(); - } - //displays error box if unable to start server - catch (SocketException e) - { - MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error); + try + { + listener = new TcpListener(IPAddress.Any, port); + listener.Start(); + listenerStarted = true; + } + //displays error box if unable to start server + catch (SocketException e) + { + MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } + //server control loop while (running) { @@ -106,10 +114,10 @@ namespace Server_application //checks if data is available on the clients stream if (inStream.DataAvailable) { + message = reader.ReadLine(); //sends data to all clients in chat foreach (KeyValuePair c in clientList) { - message = ""; string clientName; //creates StreamWriter for current outgoing client @@ -117,7 +125,6 @@ namespace Server_application writer = new StreamWriter(outStream); //appends sender name to beginning of message clientList.TryGetValue(thisClient, out clientName); - message = reader.ReadLine(); //writes sender and message to outgoing stream writer.WriteLine(clientName + ": " + message.ToString()); writer.Flush(); diff --git a/Server application/Server application/MainServer.cs b/Server application/Server application/MainServer.cs index 9e8aac2..ba421f5 100644 --- a/Server application/Server application/MainServer.cs +++ b/Server application/Server application/MainServer.cs @@ -13,6 +13,9 @@ namespace Server_application { class MainServer : TCPServer { + protected static TcpListener listener = null; + protected static bool listenerStarted = false; + //dictionary of connected clients by clientID private Dictionary clientList = new Dictionary(); private bool running = true; @@ -23,17 +26,20 @@ namespace Server_application public override void startServer() { - //attempts to start listener on chat port - try + //attempts to start listener on chat port + if (!listenerStarted) { - //possible issues with conflicts if multiple chat servers running, to be fixed later - listener = new TcpListener(IPAddress.Any, port); - listener.Start(); - } - //displays error box if unable to start server - catch (SocketException e) - { - MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error); + try + { + //possible issues with conflicts if multiple chat servers running, to be fixed later + listener = new TcpListener(IPAddress.Any, port); + listener.Start(); + } + //displays error box if unable to start server + catch (SocketException e) + { + MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } //main server control while (running)