[英]Why can't my BufferedInputStream.read() receive -1?
Hy guys I've been trying to write a program that uses TCP to transmit JSON strings. 大家好,我一直在尝试编写一个使用TCP传输JSON字符串的程序。 The problem is that when I call
BufferedInputStream.read()
I can't receive any -1
to indicate the end of a JSON string. 问题是当我调用
BufferedInputStream.read()
我不会收到任何-1
来表示JSON字符串的结尾。
My server code is: 我的服务器代码是:
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());
}
}
My client code is: 我的客户代码是:
public void sendStart(OutputStream out) throws IOException {
String jst = makeStart();
out.write(jst.getBytes());
System.out.println("data sent: " + jst);
}
When I was debugging it I saw that there was data that baos.write(i);
当我调试它时,我发现有数据包含在
baos.write(i);
wrote into i
. 写给
i
。 However the it seems like there is no -1
in the data so I can only see jst = baos.toString();
但是,似乎数据中没有
-1
,所以我只能看到jst = baos.toString();
either I terminate the client or wait for the time out. 我要么终止客户端,要么等待超时。
This is what I got: 这就是我得到的:
{"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)
The exception is because of the unexpected '{', which should be separated from the former JSON string but it didn't. 例外是由于意外的'{',应将其与以前的JSON字符串分开,但没有。
Can someone tell me where is the problem? 有人可以告诉我问题出在哪里吗? Thanks a lot!
非常感谢!
A ImputStream.read() call on a TCP stream doesn't return -1 once the message ends, instead it is waiting for a new byte (in fact, such a stream only returns -1 if it is closed). 消息结束后,TCP流上的ImputStream.read()调用不会返回-1,而是等待新的字节(实际上,这样的流仅在关闭时返回-1)。
In order to achieve the functionality you want, you could use a string termination character in the end (\\0) if you don't use it somewhere else in your message or you could send the size of the JSON-message before you send it itself and read in that size. 为了实现所需的功能,如果不在消息中的其他地方使用字符串结尾字符,则可以在结尾(\\ 0)使用字符串终止字符,也可以在发送JSON消息之前先发送其大小并以该大小阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.