Note that there are some explanatory texts on larger screens.

plurals
  1. POPrintWriter over Socket OutputStream Causes Corruption/Loss of Data
    primarykey
    data
    text
    <p>I'm trying to send a string from a server that has a default character encoding of <em>UTF-8</em> to a client that has a default character encoding of <em>windows-1252</em> via a <strong>Socket</strong> and <strong>PrintWriter</strong>.</p> <p>When I run the client below, I'm not getting my original value of 141 back, despite even my attempts at converting the String using <strong>CharsetDecoder</strong>.</p> <p>As a control test, I've tried running both these classes in Eclipse and enforcing that both use UTF-8 as their default encoding system via the dialogue below-- and I've observed that when both clients are using UTF-8, the output is successfully interpreted on the client's end.</p> <p><strong>Update</strong>: It looks like I'm able to stream bytes and recover the initial format, but in order to do so, I would have to know the encoding being used on the server. Is there not some kind of library that would be useful in this situation? I would much rather not be forced to transmit data in the form of byte arrays.</p> <p><img src="https://i.stack.imgur.com/ENU5g.png" alt="Eclipse Dialog"></p> <p>Server:</p> <pre><code>import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; //Runs on a server with default character encoding of UTF-8 public class TestServer { public static void main(String[] args) throws Exception { PrintWriter writer = null; ServerSocket serverSocket = null; try { int x = 141; String s = "#" + (char)x; serverSocket = new ServerSocket(5555); Socket clientSocket = serverSocket.accept(); writer = new PrintWriter( (new OutputStreamWriter(clientSocket.getOutputStream())), true); System.out.println((int)s.charAt(1)); writer.write(s); } catch(Exception e) { e.printStackTrace(); } finally { writer.close(); serverSocket.close(); } } } </code></pre> <hr> <p>Client:</p> <pre><code>import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; //Runs on a server with default character encoding of windows-1252 public class TestClient { public static void main(String[] args) throws Exception { Socket s = new Socket("localhost", 5555); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); String string = reader.readLine(); System.out.println((int)string.charAt(1)); //prints 194 when it was 141 on the other end //Charset.defaultCharset returns windows-1252 CharsetDecoder decoder = Charset.defaultCharset().newDecoder(); CharBuffer buffer = decoder.decode(ByteBuffer.wrap(string.getBytes())); String convertedString = buffer.toString(); System.out.println((int)convertedString.charAt(1)); //still prints 194 String convertedString2 = new String(string.getBytes(), "UTF-8"); System.out.println((int)convertedString2.charAt(1)); //prints 65533 ?? s.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload