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) { m_DISPLAYTEXT= m_strTemp.Substring(0,ndx); m_strTemp = m_strTemp.Substring(ndx + 1); } else if ( ch == SB ) { m_DISPLAYTEXT= m_strTemp.Substring(0,ndx); ldx = m_strTemp.IndexOf(Convert.ToString(SE)); m_strOption = m_strTemp.Substring(ndx, ldx); m_ListOptions.Add(m_strOption); m_strTemp = m_strTemp.Substring(ldx); } } else { m_DISPLAYTEXT = m_DISPLAYTEXT + m_strTemp; bScanDone = true ; } } m_strNormalText = m_DISPLAYTEXT; } catch(Exception eP) { MessageBox.Show(eP.Message , "Application Error!!!" , MessageBoxButtons.OK , MessageBoxIcon.Stop ); Application.Exit(); } return m_strNormalText ; }
该代码 是c#helper上的telnet协议接受代码,自己对telnet协议解析不怎么了解,所以一直没解决telnet解析中文问题,那位大哥能不帮忙看下,更改后怎么解析中文
!谢谢!!
[解决办法]
友情UP
[解决办法]
用Encoding.Default试试看
[解决办法]
首先确认服务器端使用的什么编码,用同样的编码旧ok了!