示例#1
0
        //Start listening on the ip
        public void Listen()
        {
            IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];

            try
            {
                TcpListener = new TcpListener(ipAddress, 13);
                TcpListener.Start();
            }
            catch (Exception a)
            {
                MessageBox.Show("Error: " + a.ToString());
            }

            ListenerThread = new Thread(() =>
            {
                while (true)
                {
                    TcpClient tcpClient = null;

                    try
                    {
                        tcpClient = TcpListener.AcceptTcpClient();
                    }
                    catch (SocketException)
                    {
                        break;
                    };

                    clientThread = new Thread(() =>
                    {
                        ChatParticipant newClient = new ChatParticipant(tcpClient);
                        ChatRoom.Join(newClient);
                        newClient.ServeClient();
                    });

                    clientThread.Start();
                }
            });

            ListenerThread.Start();
        }
示例#2
0
 //remove a participant from chatRoom if leave
 public void Leave(ChatParticipant participant)
 {
     Participants.Remove(participant);
     participant.OnLeave   -= Leave;
     participant.OnMessage -= SendMessage;
 }