简体   繁体   English

从 TCP 套接字 Java 读取时数据显示不正确

[英]Data not displayed correctly when read from TCP socket Java

I'm new to java sockets and processes.我是 Java 套接字和进程的新手。 I'm trying to read information from a TCP socket, but when I received the data, some information was not displayed correctly.我正在尝试从 TCP 套接字读取信息,但是当我收到数据时,有些信息没有正确显示。 (I think it might be an encoding problem, the process I'm reading from is a Genivi DLT daemon). (我认为这可能是一个编码问题,我正在读取的进程是一个 Genivi DLT 守护进程)。

Here is a sample of my code:这是我的代码示例:

try (Socket socket = new Socket(hostname, port)) {

            InputStream input = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(input);

            BufferedReader br1 = new BufferedReader(reader);         

            String line;


            while((line = br1.readLine())!=null) {
                String s = br1.readLine();
                byte[] utf8Bytes = s.getBytes("UTF-8");
                if(s.contains("SYS"))
                    System.out.println(s);
            }

Here is a photo of how the data that should be displayed .这是应该如何显示数据的照片。

And here it's how it is actually displayed to the console:这是它实际显示到控制台的方式:

ÒWAVAè5ÓrQSYS ² (PathologyCollectorCPU.cpp: getLoad(...)@69): {"Name": "Core", "cpuName": "cpu0", "percent": 33, "totalUser": 1533306, "totalNice:" 0, "totalSys": 1216838, "totalIdle": 5681926}

Does anybody has an idea why this might be, or some approaches I should take in solving my problem?有没有人知道为什么会这样,或者我应该采取一些方法来解决我的问题?

Edit: After some digging I think I have found some information about the protocol, here: https://www.autosar.org/fileadmin/user_upload/standards/foundation/1-0/AUTOSAR_PRS_DiagnosticLogAndTraceProtocol.pdf at page 16. If you have anymore further advice, it would be greatly appreciated.编辑:经过一些挖掘,我想我已经找到了一些关于协议的信息,这里: https : //www.autosar.org/fileadmin/user_upload/standards/foundation/1-0/AUTOSAR_PRS_DiagnosticLogAndTraceProtocol.pdf在第 16 页。如果你有再进一步的建议,将不胜感激。

You are using TCP (Transmission Control Protocol) and it sends data over the network by a stream.您正在使用 TCP(传输控制协议),它通过流通过网络发送数据。 So you need to define the "data-length" for each package you sent.所以你需要为你发送的每个包定义“数据长度”。

When the client/service receives that data stream, it bases on the "data-length" value to read exactly the number of bytes for a package.当客户端/服务接收到该数据流时,它根据“数据长度”值准确读取包的字节数。

You can check out the example below for more detail:您可以查看下面的示例以了解更多详细信息:

private void __updateRecvHeaderData(byte[] bytes) {
    if (bytes.length >= Constants.HEADER_BYTES) { // header length
        byte[] header = Arrays.copyOfRange(bytes, 0, Constants.HEADER_BYTES);
        __dataSize = MessagePacker.byteToShort(header); // network to host short
        __recvHeader = false;
        // package = |2 bytes header| <content bytes> |
        byte[] data = Arrays.copyOfRange(bytes, Constants.HEADER_BYTES, __dataSize + Constants.HEADER_BYTES);
        __onRecvData(data); // recursion
    }
}

The file class can be found here: TCP receive and split the stream data by data-length文件类可以在这里找到: TCP receive and split the stream data by data-length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM