→ chchwy:你這樣丟誰有辦法幫你看orz... 12/25 23:00
不好意思,又來煩大家了
我寫了一支程式來做telnet連線的動作
並將接收到的資料列印在console
但卡在連上telnet server後(Unix)
原本應該列印出"login:"的地方
卻列印出像是"?" "%"這類的亂碼
我直覺是Encoding的問題
所以將所有編碼方法全部試過,Unicode,ASCII,UTF7,UTF8,甚至default...
但都無法解決問題
如果我將server ip改為ptt.cc的話
則編碼選default就可以顯示進站畫面,其他編碼方式則還是顯示亂碼...
請問有熟悉這方面的前輩可以給點意見嗎? =.=
我的code如下:
public class TcpClientSimple
{
public int Port;
public string IP = "";
public int TimeOut;
private TcpClient tcpClient;
public TcpClientSimple()
{
}
public TcpClientSimple(string strIP,int intPort,int intTimeOut)
{
SetTCP(strIP, intPort, intTimeOut);
}
public void Close()
{
tcpClient.Close();
}
public void SetTCP(string strIP, int intPort, int intTimeOut)
{
IP = strIP;
Port = intPort;
TimeOut = intTimeOut;
}
public void Send(string strCMD,out string strReceive, out string strError)
{
strError = "";
strReceive = "";
tcpClient = new TcpClient();
System.Diagnostics.DefaultTraceListener Deg = new
System.Diagnostics.DefaultTraceListener();
try
{
tcpClient.SendTimeout = TimeOut;
tcpClient.Connect(IP, Port);
NetworkStream networkStream = tcpClient.GetStream();
if (networkStream.CanWrite && networkStream.CanRead)
{
Byte[] sendBytes = Encoding.ASCII.GetBytes(strCMD);
networkStream.Write(sendBytes, 0, sendBytes.Length);
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
int BytesRead = networkStream.Read(bytes, 0,
(int)tcpClient.ReceiveBufferSize);
string returndata = Encoding.ASCII.GetString(bytes, 0, BytesRead);
Console.WriteLine(returndata);
networkStream.Close();
tcpClient.Close();
strReceive = returndata;
}
else if (!networkStream.CanRead)
{
tcpClient.Close();
}
else if (!networkStream.CanWrite)
{
tcpClient.Close();
}
}
catch (SocketException SE)
{
strError= SE.Message;
}
catch (System.IO.IOException IOE)
{
strError= IOE.Message;
}
catch (Exception e)
{
strError= e.Message;
}
}
}
static void Main()
{
TcpClientSimple tcp = new TcpClientSimple("192.168.1.1", 23, 10);
string strR;
string strE;
tcp.Send("CMD", out strR, out strE);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.251.181.55
※ 編輯: sabertooth 來自: 111.251.181.55 (12/16 19:22)