c++发送的消息默认编码是GBK,而java消息的默认编码是UTF-8,如果二者直接收发消息的不进行转换的话,显示的中文字符都是乱码。所以Android客户端在收发消息的时候要接行编码转换。
Socket client = new Socket("10.0.2.2",4960);
DataInputStream in = new DataInputStream(client.getInputStream());
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"GBK"));
//PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"GBK")),true);
out.write("Hello,I'm Android,我是安卓!\0");
out.flush();
byte buffer[] = new byte[1024];
in.read(buffer);
String msg = new String(buffer,"GB2312").trim();
edtResult.setText(msg);
in.close();
out.close();
client.close();