日期:2014-05-18  浏览次数:21221 次

telnet协议解析中文问题
C# code

private void Receive()
        {
            //用于接收数据的缓冲
            byte[] buf;
            buf = new byte[max_char];
            int count=socket.Receive(buf);
            if (count>0)
            {
                string sRecieved = Encoding.GetEncoding("gb2312").GetString(buf, 0, count );
                
                string m_strLine="";
                for ( int i=0; i < count;i++)
                {
                    Char ch = Convert.ToChar(buf[i]);
                    switch( ch )
                    {
                        case '\r': 
                            m_strLine += Convert.ToString("\r\n"); 
                            break;
                        case '\n': 
                            break;
                        default: 
                            m_strLine += Convert.ToString(ch);
                            break;
                    } 
                }
                try
                {
                    int strLinelen = m_strLine.Length ;
                    if ( strLinelen == 0 ) 
                    {
                        m_strLine = Convert.ToString("\r\n");
                    }

                    Byte[] mToProcess = new Byte[strLinelen];
                    for ( int i=0; i <  strLinelen ; i++)
                        mToProcess[i] = Convert.ToByte(m_strLine[i]);
                    // Process the incoming data
                    string mOutText = ProcessOptions(mToProcess);
                    if ( mOutText != "" ) 
                        SetReMsg(mOutText,_sendStatus);
                    RespondToOptions();
                }
                catch( Exception ex )
                {
                    Object x = this ;
                    MessageBox.Show(ex.Message , "Information!" );
                }    
            }
            else
            {
                socket.Shutdown( SocketShutdown.Both );
                socket.Close();
            }
        }
        #endregion

        #region ProcessOptions
        private string ProcessOptions(byte[] m_strLineToProcess)
        {
            string m_DISPLAYTEXT    ="";
            string m_strTemp        ="" ;
            string m_strOption        ="";
            string m_strNormalText    ="";
            bool bScanDone            =false;
            int ndx                    =0;
            int ldx                    =0;
            char ch    ;
            try
            {
                for ( int i=0; i < m_strLineToProcess.Length ; i++)
                {
                    Char ss = Convert.ToChar(m_strLineToProcess[i]);
                    m_strTemp = m_strTemp + Convert.ToString(ss);
                }
               
                while(bScanDone != true )
                {
                    int lensmk = m_strTemp.Length;
                    ndx = m_strTemp.IndexOf(Convert.ToString(IAC));
                    if ( ndx > lensmk )
                        ndx = m_strTemp.Length;

                    if(ndx != -1)
                    {
                        m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx);
                        ch = m_strTemp[ndx + 1];
                        if ( ch == DO || ch == DONT || ch == WILL || ch == WONT ) 
                        {
                            m_strOption        = m_strTemp.Substring(ndx, 3);
                            string txt        = m_strTemp.Substring(ndx + 3);
                            m_DISPLAYTEXT+= m_strTemp.Substring(0,ndx);
                            m_ListOptions.Add(m_strOption);
                            m_strTemp        = txt ;
                        }
                        else
                            if ( ch == IAC)
                        {