简体   繁体   中英

some problem when client receive messages from server though socket

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 \� before every sentence.

Could anybody help me solve the problem?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM