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

时间:2014-10-22 20:00:18

标签: java json tcp

我一直在尝试编写一个使用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字符串分开,但它没有。

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

1 个答案:

答案 0 :(得分:1)

TCP流上的ImputStream.read()调用在消息结束后不返回-1,而是等待新字节(事实上,如果它关闭,这样的流只返回-1 )。

为了实现您想要的功能,如果您不在消息中的其他位置使用它,或者您可以发送JSON的大小,则最后可以使用字符串终止字符(\ 0)在您自己发送并读取该大小之前的消息。