private void start() { Logging.WriteAndPrintLine("Server started."); // TODO: this throws an exception when the port is in use, so that should be caught or prechecked TcpListener listener = new TcpListener(IPAddress.Any, Configuration.ServerListenPort); try { listener.Start(); } catch (System.Net.Sockets.SocketException e) { Logging.WriteAndPrintLine("Could not start listener. Is port " + Configuration.ServerListenPort + " in use?"); Logging.DumpStream(e.Message); } while (true) { Socket sock = listener.AcceptSocket(); ServerProcess sp = new ServerProcess(sock); if (serverProcesses.Count >= Configuration.MaxConnections) sp.CloseClientWithResponse("HTTP/1.0 503 Service Unavailable"); else { sp.run(); } // remove finished processes so we can keep an accurate connection count foreach (ServerProcess proc in serverProcesses) if (proc.Finished) serverProcesses.Remove(proc); } }
static void Server() { Logging.WriteAndPrintLine("Server started."); // TODO: this throws an exception when the port is in use, so that should be caught or prechecked TcpListener listener = new TcpListener(IPAddress.Any, Configuration.ServerListenPort); listener.Start(); while (true) { ServerProcess sp = new ServerProcess(listener.AcceptSocket()); sp.run(); } // 'Unreachable code detected' -> need to listen to console for 'exit' command on a different thread Logging.WriteAndPrintLine("Server shutting down."); }