从套接字的字节 [] 开头解析 int



我有一个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);

长度看起来像是小端序。 您仍然可以使用 DataInputStream,但必须交换字节。如果您使用NIO的SocketChannel和ByteBuffer,则可以设置字节顺序,但这可能更难使用。

// only do this once per socket.
DataInputStream in = new DataInputStream(
                                  new BufferedInputStream(clientSocket.getInputStream()));
// for each message.
int len0 = in.readInt();
int len = Integer.reverseBytes(len0);
assert len < 1 << 24;
byte[] bytes = new byte[len];
in.readFully(bytes);
String text = new String(bytes, "ISO-8859-1").trim();
int number = Integer.parseInt(text);

网络字节顺序又名大端序。但是看到你的数据,似乎实际上使用了小端序。至少5看起来像小端序的前 4 个字节,但在大端序中则不然。因此,您需要读取这些字节,考虑小端序并转换为long以考虑"无符号"。

public static void main(String[] args) throws IOException {
    DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(null));
    int iSize = inFromServer.readInt();
    iSize = Integer.reverseBytes(iSize); //read as little-endian
    long count = Integer.toUnsignedLong(iSize); //unsigned int
}

最新更新