示例#1
0
        public string GetResponse()
        {
            string receivedstring = "";
            string receivedchar   = "";
            int    recv           = 0;

            byte[]        receivedata = new byte[1];
            ASCIIEncoding ascii       = new ASCIIEncoding();

            do
            {
                // if this socket isnt Connected anymore, then just return -1
                if (socket.Handle.ToString() == "-1")
                {
                    return(null);
                }
                try
                {
                    // reset the one byte buffer for the next character
                    receivedata[0] = 0;
                    // get next character, a zero means there was a disconnect from the client
                    recv = socket.Receive(receivedata);
                }
                catch
                {
                    return(null);
                }
                receivedchar = ascii.GetString(receivedata, 0, receivedata.Length);
                // Handle backspace or empty characters
                if ((receivedchar == "\x08" && receivedstring.Length > 0))
                {
                    receivedstring = receivedstring.Substring(0, receivedstring.Length - 1);
                }
                // Keep building the command string if we havent hit a CR or LF yet
                if ((receivedchar != "\r" && receivedchar != "\n" && receivedchar != "\x08"))
                {
                    receivedstring = receivedstring + receivedchar;
                }
                // Client disconnect is signalled by a zero
                if (receivedchar.Substring(0, 1).ToCharArray()[0] == 0)
                {
                    Lib.Disco(this, "client disconnected");
                    //break;
                    return(null);
                }
                // Bail and return the command if we hit a newline character
            } while (receivedchar != "\n");

            // Remove all leading and trailing spaces from the string
            receivedstring = receivedstring.Trim();

            // reset ansi colors when carriage return is detected coming from the user
            Send(Lib.Ansireset);

            // Filter the player text
            ProfanityFilter.FilterMessage(ref receivedstring,
                                          this);

            return(receivedstring);
        }