搜档网
当前位置:搜档网 › c# socket中文出现乱码编码转换

c# socket中文出现乱码编码转换

第一种:以流的形式读取服务器传过来的结果

protected string GetNewMailTitle(string host, int port, string account, string domainName)
{
string newMailTitle = string.Empty;
string newMailText = "未能获取到您的新邮件。";
try
{
string queryString = "query?act=newtitle&uid=" + account + "&domain=" + domainName + "";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mysocket.Connect(ipe);
NetworkStream ns = new NetworkStream(mysocket);
StreamWriter sw = new StreamWriter(ns);
Encoding gbk=Encoding.GetEncoding("GBK");
StreamReader sr = new StreamReader(ns,gbk);
string cmdPrompt = sr.ReadLine();

if (cmdPrompt.Equals("helo"))
{
UTF8Encoding encoder = new UTF8Encoding();


byte[] sendQueryTemp = encoder.GetBytes(queryString);
byte[] sendQuery = new byte[sendQueryTemp.Length + 2];
char[] closeChar = new char[2];
closeChar[0] = (char)(10);
closeChar[1] = (char)(13);
byte[] closeByte = Encoding.UTF8.GetBytes(closeChar);

for (int i = 0; i < sendQueryTemp.Length; i++)
{
sendQuery[i] = sendQueryTemp[i];
}

sendQuery[sendQuery.Length - 2] = closeByte[0];
sendQuery[sendQuery.Length - 1] = closeByte[1];
mysocket.Send(sendQuery, 0, sendQuery.Length, SocketFlags.None);
//Encoding gbk=Encoding.GetEncoding("GBK");
byte [] unicodeBytes=gbk.GetBytes(sr.ReadLine());
byte[] utf8Bytes = Encoding.Convert(gbk, Encoding.UTF8, unicodeBytes);
newMailTitle = Encoding.UTF8.GetString(utf8Bytes);

//将GBK编码转换为UTF8编码,在页面中显示,因为页面编码是UTF8
}
else
{
// 未能连接到指定的端口
Logger.OutErrorMessage(string.Format("邮件:访问指定服务器({0}:{1})出错。", host, port));
}
sw.Close();
sr.Close();
mysocket.Close();
if (!string.IsNullOrEmpty(newMailTitle))
{
//// 判断是否是数字
//int result;
//if (!int.TryParse(newMailTitle, out result))
//{
// // 读取的邮件数不为数字
//

Logger.OutErrorMessage(string.Format("获取未读邮件数失败。读取到的信息是:{0}。查询命令是:{1}", newMailTitle, queryString));
// // 失败时显示新邮件数为0
// newMailTitle = "0";
//}
newMailText = "1邮件标题"+newMailTitle;


}
}
catch (Exception ex)
{
Logger.OutErrorMessage(ex.ToString());
}

return newMailText;

}



第二种:非流形式的读取数据

static void Main(string[] args)
{
try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
s.Bind(ipe);//绑定2000端口
s.Listen(0);//开始监听
Console.WriteLine("Wait for connect");
Socket temp = s.Accept();//为新建连接创建新的Socket。
Console.WriteLine("Get a connect");
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
recvStr += Encoding.Unicode.GetString(recvBytes, 0, bytes);

byte[] unicodeBytes = Encoding.Unicode.GetBytes(recvStr);
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, unicodeBytes);
string result = Encoding.UTF8.GetString(utf8Bytes);

//将unicode编码转换为UTF8编码,在页面中显示,因为页面编码是UTF8

recvStr = result;
Console.WriteLine("Server Get Message:{0}", recvStr);//把客户端传来的信息显示出来
string sendStr = "Ok!Client Send Message Sucessful!";
byte[] bs = Encoding.UTF8.GetBytes(sendStr);
temp.Send(bs, bs.Length, 0);//返回客户端成功信息
temp.Close();
s.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();

}


相关主题