예제 #1
0
        private void startServerButton_Click(object sender, EventArgs e)
        {
            IPAddress ipAddr = IPAddress.Any;// .Parse("127.0.0.1");        

            ChatServer mainServer = new ChatServer(ipAddr);

            // Hook the StatusChanged event handler to mainServer_StatusChanged

            ChatServer.StatusChanged += new Action<string>(mainServer_StatusChanged);
            ChatServer.UserStatusChanged += new Action<string, string>(UserStatusChanged);
            // Start listening for connections

            mainServer.StartListening();          
        }
예제 #2
0
        // Occures when a new client is accepted
        private void AcceptClient()
        {
            srReceiver = new System.IO.StreamReader(tcpClient.GetStream());
            swSender   = new System.IO.StreamWriter(tcpClient.GetStream());
            // Read the account information from the client

            string m = srReceiver.ReadLine();

            strMessage = strMessage.Deserialize(m);

            currUser = strMessage.UserName; // srReceiver.ReadLine();
            // We got a response from the client

            if (IsSuccessfullyConnected(m))
            {
                try
                {
                    // Keep waiting for a message from the user
                    while (!string.IsNullOrEmpty((strResponse = srReceiver.ReadLine())))
                    {
                        lock (sync)
                        {
                            // If it's invalid, remove the user
                            if (strResponse == null)
                            {
                                ChatServer.RemoveUser(tcpClient);
                            }
                            else
                            {
                                // Otherwise send the message to all the other users
                                strMessage = strMessage.Deserialize(strResponse);

                                ChatServer.SendMessage(currUser, strMessage.CurrentMessage);
                            }
                        }
                    }
                }
                catch
                {
                    // If anything went wrong with this user, disconnect him
                    ChatServer.RemoveUser(tcpClient);
                }
            }
        }