Skip to content Skip to sidebar Skip to footer

How To Get Server Message Correctly

Problem I send the message '12345' from the socket server to the client: myPrintWriter.println('12345'); After that I read this message on client: int c; while ((c = inputStream.r

Solution 1:

Have in mind the UTF-8 enconding may result in more than one byte per character, and your code above will fail to correctly handle them.

If you want to read String encoded in UTF-8, is better to have them decoded by a ´BufferdReader` and getting it line by line.

Example code:

String line;
    BufferedReader _in = new BufferedReader(new InputStreamReader(_socket.getInputStream(),"UTF-8"));

    try {
         while ((line = _in.readLine()) != null) {
            Log.d(TAG, line);
         }
         Log.d(TAG, "Connection is closed");
    } catch (Exception e) {
         Log.d(TAG, "Connection is closed");
    }

Regards.

Solution 2:

Definitely it is CR/LF. It is there because you are using println. Try print instead.

Post a Comment for "How To Get Server Message Correctly"