接收方法的解决:
在接收数据线程的死循环里,把接受的消息转为字符串时,加入编码格式”GB2312“
//定义全局的编码格式
public Charset TcpServerCharset = Charset.forName("GB2312");
发送方法的解决:
/**
* 循环遍历客户端集合,给每个客户端都发送信息。
*/
private void serverSendMessage() {
if (!Check()) {
return;
}
for (int i = 0, size = mSocketList.size(); i < size; i++) {
Socket socket = mSocketList.get(i);
try {
OutputStream outputStream = socket.getOutputStream();
outputStream.write(mServerSendMessage.getBytes(TcpServerCharset));
outputStream.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}