示例#1
0
        public void RemoveConnection(string id)
        {
            // получаем по id закрытое подключение
            ClientServer client = clients.FirstOrDefault(c => c.Id == id);

            // и удаляем его из списка подключений
            if (client != null)
            {
                clients.Remove(client);
            }
        }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        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();
            }
        }
示例#4
0
 public void AddConnect(ClientServer client)
 {
     clients.Add(client);
 }