【发布时间】:2011-10-25 01:47:43
【问题描述】:
在 C# 方面,我有这个代码来发送 unicode 字符串
byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
string unicode = System.Text.Encoding.UTF8.GetString(b);
//Plus \r\n for end of send string
SendString(unicode + "\r\n");
void SendString(String message)
{
byte[] buffer = Encoding.ASCII.GetBytes(message);
AsyncCallback ac = new AsyncCallback(SendStreamMsg);
tcpClient.GetStream().BeginWrite(buffer, 0, buffer.Length, ac, null);
}
private void SendStreamMsg(IAsyncResult ar)
{
tcpClient.GetStream().EndWrite(ar);
tcpClient.GetStream().Flush(); //data send back to java
}
这是Java方面
Charset utf8 = Charset.forName("UTF-8");
bufferReader = new BufferedReader(new InputStreamReader(
sockServer.getInputStream(),utf8));
String message = br.readLine();
问题是我无法在 Java 端接收 unicode 字符串。怎么解决?
【问题讨论】:
-
“收不到”是什么意思?究竟会发生什么?
-
为什么要将 utf-16 字符串转换为 utf-8 字节数组,然后再转换回 utf-16 字符串?
标签: c# java sockets unicode-string