public void HandlingMessagesClient() { clientStream = client.GetStream(); clientReader = new BinaryReader(clientStream); ClientWriter = new BinaryWriter(clientStream); var block = true; while (block) { try { var netMessageType = (NetMessageType)clientReader.ReadByte(); switch (netMessageType) { case NetMessageType.DataHosts: MainHostServer.RequestDataAboutHosts(clientID); break; } } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); block = false; CloseClient(); } } }
static void Main(string[] args) { try { serverHosts = new MainHostServer(); serverClients = new MainClientServer(); Task.Factory.StartNew(serverClients.GettingClients, TaskCreationOptions.LongRunning); Task.Factory.StartNew(serverHosts.GettingHosts, TaskCreationOptions.LongRunning); while (true) { Console.WriteLine("If you want to disconnect the main Server, press '7'!"); var key = Console.ReadKey(true); if (key.KeyChar == '7') { break; } } } catch (Exception ex) { serverHosts.DisconnectHosts(); serverClients.DisconnectClients(); Console.WriteLine(ex.Message); } }
public HostServer(TcpClient tcpClient, int port, MainHostServer mainHostServer) { host = tcpClient; hostID = Guid.NewGuid().ToString(); hostPort = port; this.mainHostServer = mainHostServer; mainHostServer.AddConnectionHost(hostID, port, this); }