我在运行一段代码时,发现只打印aaa,没有打印bbb,通过调试,发现在while一次过后,下次while时,while的那行停止了,啥反应都没有ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch;
while((ch=inputStream.read()) != -1){
System.out.println("aaa");
bos.write(ch);
}
System.out.println("bbb");

解决方案 »

  1.   

    因为阻塞住了
    InputStream.read()方法是阻塞式的,详情查API
      

  2.   

    Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown. 
      

  3.   

     你是不是捕获一个异常看看有什么问题?
    try{
       while((ch=inputStream.read()) != -1){
          System.out.println("aaa");
          bos.write(ch);
       }
    }catch(Exception e){
    ... ...
    }
      

  4.   

    ch=inputStream.read(),是阻塞式的,没反应是因为没数据到达。
      

  5.   

    我try、catch过了,没有任何的异常,只是在while循环一遍后,就在while那行停住了
      

  6.   

    while循环一次,表示读取已经结束。
    你说“下一次while”,是重新进入到这段程序吗?
    如果是的话,就表示read()在等待输入流的数据。
    阻塞是机制,并不是错误,要解决什么?。
    你检查下输入流吧,数据是否发送。
      

  7.   

    把这一行去掉试试System.out.println("aaa");
    乱猜的
      

  8.   

    linux下不行吗?这个我就不清楚了。
    我建议在报文中设置规则:
    比如按报文长度读取,
    或者是在报文末添加结束符。