gpt4 book ai didi

java - 为什么我的 BufferedInputStream.read() 不能接收 -1?

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

伙计们,我一直在尝试编写一个使用 TCP 传输 JSON 字符串的程序。问题是,当我调用 BufferedInputStream.read() 时,我无法收到任何 -1 来指示 JSON 字符串的结尾。

我的服务器代码是:

public void listen() {
try {
if (getClient() != null) {
String jst = null;
Message msg = null;
InputStream in = getClient().getInputStream();
BufferedInputStream inBuf = new BufferedInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (getClient() != null) {
System.out.println("Receiving Data...");
try {
int i;
while ((i = inBuf.read()) != -1) {
baos.write(i);
}
jst = baos.toString();
} catch (IOException e) {
System.err.println("Bad Connection.");
stopStream();
break;
}
System.out.println(jst);
msg = MessageFactory.fromJSON(jst);
if (msg.getType().equals(StartMessage.defaultType)) {
startStream((StartMessage) msg);
} else if (msg.getType().equals(StreamMessage.defaultType)) {
streaming((StreamMessage) msg);
} else if (msg.getType().equals(
StopStreamMessage.defaultType)) {
stopStream();
}
}
}
} catch (IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}

我的客户端代码是:

public void sendStart(OutputStream out) throws IOException {
String jst = makeStart();
out.write(jst.getBytes());
System.out.println("data sent: " + jst);
}

当我调试它时,我看到 baos.write(i); 写入了 i 的数据。然而,数据中似乎没有 -1 所以我只能看到 jst = baos.toString(); 我终止客户端或等待时间出。

这是我得到的:

{"format":"raw","width":320,"type":"startstream","height":240}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}
Unexpected token LEFT BRACE({) at position 62.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at au.edu.benjamin.message.MessageFactory.fromJSON(MessageFactory.java:14)
at au.edu.benjamin.transmission.TransListenerThread.listen(TransListenerThread.java:78)
at au.edu.benjamin.transmission.TransListenerThread.run(TransListenerThread.java:53)
at java.lang.Thread.run(Thread.java:745)
null
Exception in thread "Thread-2" java.lang.NullPointerException
at au.edu.benjamin.transmission.TransListenerThread.listen(TransListenerThread.java:79)
at au.edu.benjamin.transmission.TransListenerThread.run(TransListenerThread.java:53)
at java.lang.Thread.run(Thread.java:745)

异常是因为意外的'{',它应该与以前的JSON字符串分开但没有。

谁能告诉我问题出在哪里?非常感谢!

最佳答案

一旦消息结束,对 TCP 流的 ImputStream.read() 调用不会返回 -1,而是等待新字节(事实上,这样的流仅在关闭时返回 -1)。

为了实现您想要的功能,如果您不在消息的其他地方使用它,您可以在末尾使用字符串终止字符 (\0),或者您可以在之前发送 JSON 消息的大小您自己发送并以该尺寸阅读。

关于java - 为什么我的 BufferedInputStream.read() 不能接收 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26516045/

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