0

while doing some exercise on socket programming, I encounter a problem in reading Unicode.

I have tested this line outside by creating a new class that is just to input and output Vietnamese ( Unicode characters ) . And It gives the correct output as I input some Unicode characters such as ă, â, ...

        inputLine = new BufferedReader(new InputStreamReader(System.in,StandardCharsets..UTF_8));

But when I attached the above line to my socket programming class, It goes wrong and I cannot find out why.

        clientSocket = new Socket(host, portNumber);
        inputLine = new BufferedReader(new InputStreamReader(System.in,StandardCharsets..UTF_8));
        String temString = inputLine.readLine();
        os = new DataOutputStream(clientSocket.getOutputStream());
        is = new DataInputStream(clientSocket.getInputStream());

The result of temString in the above is a question mark "?" when I typed in "â", which has an int value of 65533.

Please help me fix this problem. Thanks.

Ryan_Chau
  • 233
  • 2
  • 19
  • 5
    Are you using Microsoft Windows `cmd.exe` for `System.in`? I'm not sure, but it could be that `cmd.exe` still doesn't use the same character encoding as the rest of Windows. Given your problem description, I find it quite likely that the problem is that whatever writes what you read on System.in is actually not using UTF-8. – Christian Hujer Dec 20 '14 at 12:46
  • @ChristianHujer System.in is the console in Eclipse I guess?. I'm not sure. But in my case, I code like that in Eclipse and using console in Eclipse to get input. It is correct in the normal class, but it goes wrong in the socket-programming class :( – Ryan_Chau Dec 20 '14 at 13:18
  • [Try this](http://stackoverflow.com/questions/10764920/utf-16-on-cmd-exe) or [this](http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how/388500#388500) – Richard Kenneth Niescior Dec 20 '14 at 14:08
  • @RichardKennethNiescior The question is that: how can the code above is work in one case but not work in the other. – Ryan_Chau Dec 20 '14 at 15:25

0 Answers0