简体   繁体   English

Java:从套接字读取字节流

[英]Java: read a stream of byte from a socket

Im setting up a software that send a message in byte [] and receive a stream of byte.我正在设置一个软件,该软件以字节 [] 发送消息并接收字节流。 Example, send the array示例,发送数组

byte[] replayStatuse = new byte[] { 0x55, (byte) 0xAA, 0x0B, 0x00, 0x0A, 
0x1C, 0x03, 0x41, 0x01, 0x00 }

and I receive something similar.我收到类似的东西。 I did the test using PacketSender and I can see the answer in Hex when I ask the status.我使用 PacketSender 进行了测试,当我询问状态时,我可以看到十六进制的答案。 Im using我正在使用

InputStream socketInputStream = socket.getInputStream();

I already tried the various method I found here on stack and other forum but it didnt works.我已经尝试了在堆栈和其他论坛上找到的各种方法,但没有用。 Methods like this:像这样的方法:

int read;
while((read = socketInputStream.read(buffer)) != -1)
{
   String output = new String(buffer, 0, read);
   System.out.print(output);
   System.out.flush();
}

I tried to use int read of type char, byte or other format but nothing.我尝试使用 char、byte 或其他格式的 int read,但没有使用。 In my console it prints strange char (U)在我的控制台中,它打印出奇怪的字符 (U)

Im using:我正在使用:

InputStream socketInputStream = socket.getInputStream();
socketInputStream.read();

Im expecting to get a byte[] and be able to read with the function:我希望得到一个字节 [] 并能够使用该函数读取:

System.out.println(Arrays.toString(byteArray));

So I can then handle the various cases and transform if need to a String or HEX Thank you all所以我可以处理各种情况并在需要时转换为字符串或十六进制谢谢大家

The character 'U' is not strange, it is the ASCII character for the hexadecimal value of 0x55 (ie the same as your first value in the test array).字符“U”并不奇怪,它是十六进制值 0x55 的 ASCII 字符(即与您在测试数组中的第一个值相同)。 The next few values in the array may be throwing off the print statement.数组中接下来的几个值可能会丢弃打印语句。 I recommend checking/displaying the length of 'buffer' so you know how many bytes were placed into the array.我建议检查/显示“缓冲区”的长度,以便您知道数组中放入了多少字节。

I'm not sure I understand your problem completely but lets try我不确定我是否完全理解你的问题,但让我们试试

From the beginning you have a source of bytes, I will assume the size is unknown.从一开始你就有一个字节来源,我假设大小是未知的。

byte[] buffer = new byte[4096]; //I assume you have something like this

//Lets use this to accumulate all the bytes from the inputstream
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

int read;
while((read = socketInputStream.read(buffer)) != -1)
{
    byteStream.write(buffer, 0, read); //accumulates all bytes
}

byteStream.flush(); //writes out any buffered byte

byte[] allBytesRead = byteStream.toByteArray(); //All the bytes read in an array

This is all the bytes that were sent.这是发送的所有字节。 Say you want to print each byte in hex假设你想以十六进制打印每个字节

for(byte b : allBytesRead) {
    //might not be a good ideia if its something big. 
    //build a string with a StringBuilder instead.
    System.out.println(String.format("%02X", b));
}

Now it's up to you how you will process these bytes.现在由您决定如何处理这些字节。

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

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