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

标签 java sockets tcp bytearray

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

相关文章:

java - 只是另一个网络创业公司 - 平台比较

c - 在 Linux C 程序中的非特权套接字(非原始套接字)上设置 TCP 选项

c# - 使用浏览器发送modbus命令

TCP 缓冲区大小和以太网绑定(bind)

c - getline() 函数行为异常

c# - TcpListener TcpClient 获取 IP 地址

java - 策略设计模式

java - 如何在 Spring Boot 中从端点响应中全局省略空 xml 标签?

java - couchbase 中的版本控制概念

c- recvfrom 错误 22