TCP IP端口中不可读字符串的转换在Java中。

huangapple 未分类评论44阅读模式
英文:

Conversion of unreadable string from TCP IP port in java

问题

以下是您要求的翻译部分:

我正在尝试使用Java Socket编程ServerSocket从系统的TCP IP端口读取连续数据,但我得到的是像下面这张图片中一样的无法读取的字符串数据:

(图片链接)
1: https://i.stack.imgur.com/3i5kY.png

我正在使用以下代码从TCP IP端口读取数据:

ServerSocket server_socket;
BufferedReader input;
try {
    server_socket = new ServerSocket(6666);
    LOGGER.info("Server waiting for client on port "
            + server_socket.getLocalPort());
    System.out.println("Server waiting for client on port "
            + server_socket.getLocalPort());
    while (true) {
        Socket socket = server_socket.accept();

        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        try {
            while (true) {
                String message = input.readLine();
                if (message == null) {
                    break;
                }

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

因此,请告诉我上面图片中是什么类型的数据,我应该进行什么样的转换?

提前谢谢。

英文:

I am trying to read continuous data from TCP IP port of system using Java Socket Programming ServerSocket but I am getting unreadable string data like in following following image :

TCP IP端口中不可读字符串的转换在Java中。

I am using following code for read data from TCP IP port :

 ServerSocket server_socket;
            BufferedReader input;
            try {
                server_socket = new ServerSocket(6666);
                LOGGER.info("Server waiting for client on port "
                        + server_socket.getLocalPort());
                System.out.println("Server waiting for client on port "
                        + server_socket.getLocalPort());
                while (true) {
                    Socket socket = server_socket.accept();

                    input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    try {
                        while (true) {
                            String message = input.readLine();
                            if (message == null) {
                                break;
                            }

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

So please let me know what kind of data are in above image and what conversion I should do?

Thanks in advance.

答案1

得分: 0

一处缺陷:

InputStreamReader 负责将二进制数据(InputStream)转换为读取器((Unicode)文本)。为此,您可以并且应该指定一个文本编码,即 Charset。您正在使用默认的平台编码。

input = new BufferedReader(
            new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));

您现在可以通过 telnet 或其他终端工具访问此服务器应用程序。甚至可以通过 http://localhost:6666/telnet://localhost:6666/?)在浏览器中访问。

您所看到的内容似乎是非文本二进制数据 - 默认编码是否不是中文或类似的。

此外,通过从线程池中获取一个新线程来处理来自 acceptsocket,可以改进代码。

应注意发送的头行(您未输出)以及之后的接收。

英文:

One flaw:

The InputStreamReader is responsible for converting binary data (an InputStream) to a Reader ((Unicode) text). For that you can & should specify a text encoding, a Charset. You are using the default, platform encoding.

input = new BufferedReader(
            new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));

You can now access this server application yourself by telnet or an other terminal tool. Or even the browser by http://localhost:6666/ (telnet://localhost:6666/?).

What you are seeing does look like non-text binary data - should the default encoding not be Chinese or such.

Furthermore the code would be improved by a new thread from a thread pool handling the socket from the accept.

One should pay attention to the header lines one sends (you do not output) and then receive.

huangapple
  • 本文由 发表于 2020年7月26日 01:39:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/63091509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定