public ServerClient(TcpClient client, MessageRouter router) { this.client = client; this.router = router; dbCon = new DBController("pbarco", "12345Pablo"); service = new CommunicationService(this.client.GetStream(), this); // Subscribe Events service.Connect += service_Connect; service.Disconnect += service_Disconnect; service.LocationChanged += service_LocationChanged; service.DBRequested += service_DBRecuested; sending = new Thread(sendMessages); sending.Start(); }
static void Main(string[] args) { Console.SetWindowSize(120, 35); string ip = GetLocalIPAddress(); int port = 8999; CancellationTokenSource cts = new CancellationTokenSource(); TcpServer server = new TcpServer(ip, port); MessageRouter router = new MessageRouter(cts.Token); Thread runningServer = new Thread(() => { try { server.StartServer(); while (true) { TcpClient tcpclient = server.AcceptClient(); Console.WriteLine("Cliente nuevo"); ServerClient client = new ServerClient(tcpclient, router); router.AddClient(client); } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("== ERROR == -" + e.Message); Console.ForegroundColor = ConsoleColor.White; } }); try { runningServer.Start(); Console.WriteLine("Server running on IP: " + ip + ", PORT: " + port + "."); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("== ERROR == -" + e.Message); Console.ForegroundColor = ConsoleColor.White; } string command; while (true) { command = Console.ReadLine(); if (command == "shutdown") { //Console.Beep(); cts.Cancel(); router.WaitToFinish(); Environment.Exit(0); } } }