Ejemplo n.º 1
0
        public static void SendMsgToAll(string nick, string msg)
        {
            //create a StreamWriter Object
            StreamWriter writer;
            ArrayList    ToRemove = new ArrayList(0);

            //create a new TCPClient Array
            Chat.Sockets.TcpClient[] tcpClient = new Chat.Sockets.TcpClient[ChatServer.nickName.Count];
            //copy the users nickname to the CHatServer values
            ChatServer.nickName.Values.CopyTo(tcpClient, 0);
            //loop through and write any messages to the window
            for (int cnt = 0; cnt < tcpClient.Length; cnt++)
            {
                try
                {
                    //check if the message is empty, of the particular
                    //index of out array is null, if it is then continue
                    if (msg.Trim() == "" || tcpClient[cnt] == null)
                    {
                        continue;
                    }
                    //Use the GetStream method to get the current memory
                    //stream for this index of our TCPClient array
                    writer = new StreamWriter(tcpClient[cnt].GetStream());
                    //white our message to the window
                    writer.WriteLine(nick + ": " + msg);
                    //make sure all bytes are written
                    writer.Flush();
                    //dispose of the writer object until needed again
                    writer = null;
                }
                //here we catch an exception that happens
                //when the user leaves the chatroow
                catch (Exception e44)
                {
                    e44 = e44;
                    string str = (string)ChatServer.nickNameByConnect[tcpClient[cnt]];
                    //send the message that the user has left
                    ChatServer.SendSystemMessage("** " + str + " ** Has Left The Room.");
                    //remove the nickname from the list
                    ChatServer.nickName.Remove(str);
                    //remove that index of the array, thus freeing it up
                    //for another user
                    ChatServer.nickNameByConnect.Remove(tcpClient[cnt]);
                }
            }
        }
Ejemplo n.º 2
0
 private void runChat()
 //use a try...catch to catch any exceptions
 {
     try
     {
         //set out line variable to an empty string
         string line = "";
         while (true)
         {
             //read the curent line
             line = reader.ReadLine();
             //send our message
             ChatServer.SendMsgToAll(nickName, line);
         }
     }
     catch (Exception e44)
     {
         Console.WriteLine(e44);
     }
 }