public static int Main(String[] args) { // configure logging var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); // kick off the server, if nothing is passed in default to localhost ServerMain theServer = new ServerMain(); RunListenerLoop(theServer); return(0); }
public static void RunListenerLoop(ServerMain server) { try { logger.Info($"Server operating on: {server.HostURL}"); IPHostEntry host = Dns.GetHostEntry(server.HostURL); IPAddress ipAddress = host.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, server.Port); Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); listener.Listen(10); Socket incomingClient; while (true) { try { logger.Info("Waiting for a connection..."); incomingClient = listener.Accept(); // create a client object for each incoming connection and spin off a new redis connection for each logger.Info("ServerMain received connection."); ConnectionToClient client = new ConnectionToClient(logger, server.redisMuxor.GetDatabase(), server.redisMuxor.GetSubscriber()); client.StartClient(incomingClient); logger.Info($"ServerMain created and started connection for: {incomingClient.RemoteEndPoint}"); }catch (Exception e) { logger.Info("Caught Exception while listening and creating new connections to clients"); logger.Error(e.ToString()); } } } catch (Exception e) { logger.Error(e.ToString()); } logger.Info("Press any key to continue..."); }