// Обработка сообщений от пользователя private void ListenChatter(ChatParticipant chatter) { while (chatter.alive) { if (chatter.stream.DataAvailable) { string data = chatter.ReceiveMessage(); string message = data; message = chatter.getMessage(message); int code = chatter.getCode(data); switch (code) { case CONNECT: break; case MESSAGE: DisplayMessage(message, true); break; case DISCONNECT: chatter.alive = false; Chatters.Remove(chatter); chatter.Dispose(); DisplayMessage(message, true); break; case SENDHISTORY: if (!getHistory) { getHistory = true; string temp = chatter.getText(message); DisplayMessage(temp, false); } break; default: break; } } } }
// Ожидание подключений private void ListenForConnections() { tcpListener = new TcpListener(localIP, tcp_port); tcpListener.Start(); while (alive) { if (tcpListener.Pending()) { var chatter = new ChatParticipant(); chatter.tcpClient = tcpListener.AcceptTcpClient(); chatter.IPv4Address = ((IPEndPoint)chatter.tcpClient.Client.RemoteEndPoint).Address; chatter.stream = chatter.tcpClient.GetStream(); chatter.SendMessage(" подключился!", username, localIP.ToString(), CONNECT); Chatters.Add(chatter); Task.Factory.StartNew(() => ListenChatter(chatter)); } } tcpListener.Stop(); }