gpt4 book ai didi

java - 从套接字的 byte[] 开头解析 int

转载 作者:可可西里 更新时间:2023-11-01 02:44:51 26 4
gpt4 key购买 nike

我有一个 Java 应用程序正在从接收不同大小的 XML 的 TCP 套接字读取数据。给定数据包的前 5 个字节应该指示剩余消息的大小。如果我手动创建一个大字节 [] 并读取数据,我可以成功读取消息和 xml。

以下是生成数据的应用程序手册中的说明:

Each message is preceded by the message size indicator which is a 32-bit unsinged integer using the network bytes order method. For example: \x05\x00\x00\x00\x30\x31\x30\x32\x00 indicates the message size of an ack which is 5 bytes included the fifth message byte '\0'. The size indicator specifies everything following the size indicator itself.

但是我无法弄清楚如何将前 5 个字节解码为一个整数,我可以使用它来正确调整 byte[] 的大小以读取消息的其余部分。我得到随机结果:

这是我用来解析消息的代码:

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);

关于java - 从套接字的 byte[] 开头解析 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com