0

I'm developing a socket server that will receive data from a Chinese tracking equipment, the TK-06A. So did my server that is receiving the following information:

 �L�l}�8���

What do you think you can be? What enconde I use to resolve this question? Below my code I'm using to test.

 public static void main(String[] args) {
    try {
        InputStream input = null;
        int charsRead = 0;
        //Criando um servidor que atendera na porta 8080
        int port = 5016;
        char[] inputChars = new char[1024];
        ServerSocket server = new ServerSocket(port);
        while (true) {
            try {
                System.out.println("Aguardando conexao!!!");
                Socket socket = server.accept();
                InputStreamReader isr = new InputStreamReader(socket.getInputStream());
                BufferedReader inputStream = new BufferedReader(isr);
                int data = 0;
                System.out.println("Reading from stream:");
                if ((charsRead = inputStream.read(inputChars)) != -1) {
                    System.out.println("Chars read from stream: " + charsRead);
                    System.out.println(inputChars);
                    System.out.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 Answers1

0

You should supply the encoding parameter, otherwise it defaults to the current operating system encoding = non-portable.

InputStreamReader isr =
    new InputStreamReader(socket.getInputStream(), encoding);

As encoding one could try StandardCharsets.UTF_8 (Charset), UTF_16LE and the Chinese BIG encodings (as String).

The class InputStreamReader is the bridge from binary data (InputStream, byte[]) to text (Reader, String, char) - which in java always is Unicode, to mix all scripts.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138