public void RemoveConnection(string id) { // получаем по id закрытое подключение ClientServer client = clients.FirstOrDefault(c => c.Id == id); // и удаляем его из списка подключений if (client != null) { clients.Remove(client); } }
public void SendMessage(ClientServer from, string toUsername, string messageContent) { string message = from.Username + ": " + messageContent; foreach (ClientServer c in clients) { if (c.Username == toUsername) { c.SendMessage(message); } } }
public void Listen() { try { tcpListener = new TcpListener(IPAddress.Any, 2020); tcpListener.Start(); Console.WriteLine("Server start..."); while (true) { TcpClient tcp = tcpListener.AcceptTcpClient(); ClientServer client = new ClientServer(tcp, this); Thread thread = new Thread(new ThreadStart(client.Process)); thread.Start(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Disconnect(); } }
public void AddConnect(ClientServer client) { clients.Add(client); }