【发布时间】:2016-05-02 08:15:44
【问题描述】:
我有一个 Java 应用程序正在从接收不同大小的 XML 的 TCP 套接字读取数据。给定数据包的前 5 个字节应该指示剩余消息的大小。如果我手动创建一个大字节 [] 并读取数据,我可以成功读取消息和 xml。
以下是生成数据的应用程序手册中的说明:
每条消息前面都有消息大小指示符,它是 使用网络字节顺序方法的 32 位无符号整数。为了 例如:\x05\x00\x00\x00\x30\x31\x30\x32\x00 表示消息 5 个字节的 ack 的大小包括第五个消息字节“\0”。这 大小指示符指定大小指示符之后的所有内容 自己。
但是我不知道如何将前 5 个字节解码为一个整数,我可以使用该整数来正确调整字节 [] 的大小以读取消息的其余部分。我得到随机结果:
这是我用来解析消息的代码:
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
BufferedInputStream inFromServer = new BufferedInputStream(clientSocket.getInputStream());
byte[] data = new byte[10];
inFromServer.read(data);
String result = new String(data, "ISO-8859-1");
Logger.info(data+"");
//PROBLEM AREA: Tried reading different byte lengths but no joy
//This should be a number but it never is. Often strange symbols
byte[] numeric = Arrays.copyOfRange(data,1,5);
String numericString = new String(numeric, "ISO-8859-1");
//Create a huge array to make sure everything gets captured.
//Want to use the parsed value from the start here
byte[] message = new byte[1000000];
inFromServer.read(message);
//This works as expected and returns correctly formatted XML
String fullMessage = new String(message, "ISO-8859-1");
Logger.info("Result "+result+ " Full message "+fullMessage);
【问题讨论】:
-
消息长度在前四个而不是五个字节
-
“网络字节顺序”看起来很像 little-endian,也称为 not 网络字节顺序。
-
说明不正确。这不是网络字节顺序中的 5。如果是,您可以使用
DataInputStream.readInt()。事实上,您应该向供应商投诉(“寻求澄清”)。这不是 XML。
标签: java sockets tcp bytearray