C#Socket传送/接收中文出现乱码的解决办法
来源:互联网 发布:c语言西北工业大学 编辑:程序博客网 时间:2024/06/09 22:56
我的程式采用Base64编码方式
1.Client
try
{
TcpClient client = new TcpClient();
Console.WriteLine("Connecting.....");
client.Connect("172.18.33.85",8001);
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
string str = Console.ReadLine();
Stream stream = client.GetStream();
//转换发送字串为Base64编码
byte[] bytes1 = Encoding.GetEncoding("BIG5").GetBytes(str);
string encode = Convert.ToBase64String(bytes1);
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] bytes2 = ascii.GetBytes(encode);
Console.WriteLine("Transmitting......");
stream.Write(bytes2,0,bytes2.Length);
byte[] bytes = new byte[100];
int j = stream.Read(bytes,0,100);
for (int i=0;i<j;i++)
{
Console.Write(Convert.ToChar(bytes[i]));
}
client.Close();
}
catch(Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
Console.ReadLine();
2.Server
try
{
IPAddress ip = IPAddress.Parse("172.18.33.85");//这里是你的ip
TcpListener myListener = new TcpListener(ip,8001);
myListener.Start();
Console.WriteLine("Server is running at port 8001...");
Console.WriteLine("The local End point is :" + myListener.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket mySocket = myListener.AcceptSocket();
Console.WriteLine("Connection accepted from " + mySocket.RemoteEndPoint);
byte[] bytes1 = new byte[100];
int j = mySocket.Receive(bytes1);
Console.WriteLine("Recieved...");
string encode = "" ;
for (int i=0;i<j;i++)
{
encode += Convert.ToChar(bytes1[i]);
}
//还原Base64编码的接收字串
byte[] bytes2 = Convert.FromBase64String(encode);
string decode = Encoding.GetEncoding("BIG5").GetString(bytes2);
Console.WriteLine(decode);
ASCIIEncoding ascii = new ASCIIEncoding();
mySocket.Send(ascii.GetBytes("The string was recieved by the server."));
Console.Write("Sent Acknowledgement");
mySocket.Close();
myListener.Stop();
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
- C#Socket传送/接收中文出现乱码的解决办法
- C# : Socket 接收中文显示乱码的解决办法
- Java socket接收出现中文乱码
- Java socket接收出现中文乱码
- socket传输中文出现乱码的解决
- LoadRunner中文出现乱码的解决办法
- LoadRunner中文出现乱码的解决办法
- LoadRunner中文出现乱码的解决办法
- HTML中文出现乱码的解决办法
- SpringMVC Controller 接收页面传递的中文参数出现乱码
- SpringMVC Controller 接收页面传递的中文参数出现乱码
- Java-Socket接收服务端数据--中文乱码
- socket 接收浏览器post中文编码解决办法
- php接收ajax中文,后台乱码解决办法
- eclipse、myEclipse中接收中文乱码解决办法
- 用request接收中文出现乱码
- 浏览器传递中文,服务端接收 出现乱码
- 浏览器传递中文,服务器接收出现乱码
- 看上一本书
- 06.01.2005 劲风外经贸打球日记——对付高手就要欲判
- 控件之美_常用业务组件写法
- [原创]再谈实时演算布料的建模方式。
- 编程一些问题及其解决方法
- C#Socket传送/接收中文出现乱码的解决办法
- web组合框的实现方法
- code clip
- 有些歌你一定听过,但却说不出名字。
- vb6 鼠标滚轮支持 插件
- Ioc模式解析
- E-mail Icon
- 常用免费软件
- 如何动态调用WebService?