コード例 #1
0
        private void startChat()
        {
            //create our StreamReader object to read the current stream
            reader = new System.IO.StreamReader(client.GetStream());
            //create our StreamWriter objec to write to the current stream
            writer = new System.IO.StreamWriter(client.GetStream());
            writer.WriteLine("Welcome to PCChat!");
            //retrieve the users nickname they provided
            nickName = GetNick();
            //check is the nickname is already in session
            //prompt the user until they provide a nickname not in use
            while (ChatServer.nickName.Contains(nickName))
            {
                //since the nickname is in use we display that message,
                //then prompt them again for a nickname
                writer.WriteLine("ERROR - Nickname already exists! Please try a new one");
                nickName = GetNick();
            }
            //add their nickname to the chat server
            ChatServer.nickName.Add(nickName, client);
            ChatServer.nickNameByConnect.Add(client, nickName);
            //send a system message letting the other user
            //know that a new user has joined the chat
            ChatServer.SendSystemMessage("** " + nickName + " ** Has joined the room");
            writer.WriteLine("Now Talking.....\r\n-------------------------------");
            //ensure the buffer is empty
            writer.Flush();
            //create a new thread for this user
            Thread chatThread = new Thread(new ThreadStart(runChat));

            //start the thread
            chatThread.Start();
        }
コード例 #2
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]);
                }
            }
        }