3

I need to receive a unicode (UTF-8) string sent by client on a server side. The length of the string is of course unknown.

ServerSocket serverSocket = new ServerSocket(567);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

I can read bytes using in.read() (until it returns -1) but the problem is that the string is unicode, in other words, every character is represented by two bytes. So converting the result of read() which would work with normal ascii characters makes no sense.

update

As per suggestions bello, I created the reader as follows:

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));

I've changed the client side to send a newline (#10#13) after each string. But the new problem is I get bullshit instead of real string if i call:

in.readLine();

And print the result I get some nonsense string (I cannot even copy it here) although I am not dealing with non-latin chars or anything else.

To see what's going on I introduced following code:

int j = 0
while (j < 255){
    j++;
   System.out.print(in.read()+", ");
}

So here I just print all bytes received. If I send "ab" I get:

97, 0, 98, 0, 10, 13, 

This is what one would expect, but than why the readLine method doesn't produce "good" results? Anyway, if we couldn't find the actual answer, I should probably collect the bytes (like above) and create my string from them? How to do that?

P.S. Just a quick note - I am on windows.

3 Answers 3

4

Use new InputStreamReader(clientSocket.getInputStream(), "UTF-8") in order to set properly the name of the charset to use while reading the InputStream coming from your client

3

When creating InputStreamReader you can set encoding like this:

BufferedReader in = 
     new BufferedReader(
         new InputStreamReader(clientSocket.getInputStream(), "UTF-8")
);
4
  • This solution seems valid, but I have some problem. I send a string terminated by \10\13 from the client and on the server side I do System.out.println(in.readLine()); however, what I receive is compleet gibberish. The interesting thing is if I read data byte by byte it seems that everything is okay (e.g. AB is encoded like 65,0,66,0 or like that). Only the very first letter of my string is correct and all others are just unreadable.
    – Gonzalez
    Apr 18, 2016 at 16:00
  • On a client side instead of sending \10\13, try sending two chars: \u000D and \u000A .
    – user987339
    Apr 18, 2016 at 16:24
  • The problem is that client is in C++ and I know very little about that language. Any way I'll try to find how to do so.
    – Gonzalez
    Apr 18, 2016 at 16:26
  • I updated the first post including some research about my problem.
    – Gonzalez
    Apr 18, 2016 at 19:42
2

Try this way:

    Reader in = new BufferedReader(
                 new InputStreamReader(
                    clientSocket.getInputStream(), StandardCharsets.UTF_8));

Note the StandardCharsets class. It is supported since Java 1.7 and provides more elegant way to specify a standard encoding like UTF-8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.