title: How to read unicode characters from server socket
body: <p>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.</p>
  <pre><code>ServerSocket serverSocket = new ServerSocket(567);
  Socket clientSocket = serverSocket.accept();
  PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  </code></pre>
  <p>I can read bytes using <code>in.read()</code> (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 <code>read()</code> which would work with normal ascii characters makes no sense.</p>
  <h1>update</h1>
  <p>As per suggestions bello, I created the reader as follows:</p>
  <pre><code>BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),&quot;UTF-8&quot;));
  </code></pre>
  <p>I've changed the client side to send a newline (<code>#10#13</code>) after each string.
  But the new problem is I get bullshit instead of real string if i call:</p>
  <pre><code>in.readLine();
  </code></pre>
  <p>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.</p>
  <p>To see what's going on I introduced following code:</p>
  <pre><code>int j = 0
  while (j &lt; 255){
      j++;
     System.out.print(in.read()+&quot;, &quot;);
  }
  </code></pre>
  <p>So here I just print all bytes received. If I send &quot;ab&quot; I get:</p>
  <pre><code>97, 0, 98, 0, 10, 13, 
  </code></pre>
  <p>This is what one would expect, but than why the readLine method doesn't produce &quot;good&quot; 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>
  <p>P.S. Just a quick note - I am on windows.</p>