some problem when client receive messages from ser

2019-07-11 02:25发布

in my server code:

pw = new PrintWriter(stream);
br = new BufferedReader(new InputStreamReader(
    socket.getInputStream(), "UTF-8"));

in my client code

BufferedReader br = new BufferedReader(new InputStreamReader(
                    clientSocket.getInputStream(), "UTF-8"));
OutputStreamWriter stream = new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8");
PrintWriter pw = new PrintWriter(stream);

then at first, server use: pw.println("OK"); pw.flush();

client receive OK

After that, server use:

pw.println("TRUE")
pw.flush();

this time, client receive a string that has unknow char

\ufffd\ufffdTRUE

then i keep send msg to client and it will receive some \ufffd before every sentence.

Could anybody help me solve the problem?

1条回答
可以哭但决不认输i
2楼-- · 2019-07-11 02:55

I could imagine that the server resends byte order markers (0xFFFE 0xFFFF), which are replaced by the replacement character (0xFFFD) at the client side.
Do you re-create the print writer before each send on the server side?

查看更多
登录 后发表回答