cuiping43
cuiping43
采纳率4.2%
2016-06-07 01:04 阅读 2.1k

请问socket流什么时候能用到InputStream.available()方法

图片说明

如图,我做的是一个客户端向服务器上传图片的小程序,我调试了下,发现此处客户端可以进入while()循环,并且能读图片并写进socket流中,但是此处服务器端进不了while循环,而是在执行到is.available()!=0这个语句的时候,就catch到了IO异常,然后下面就是finally强制关闭了is输入流。
我实在是想不通了,特来求教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • aline200 aline200 2016-06-07 02:31

    具体报的啥错,贴出看看

    点赞 评论 复制链接分享
  • taiyangdao 易生一世 2016-06-07 03:01

    Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
    Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

    A subclass' implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

    The available method for class InputStream always returns 0.

    This method should be overridden by subclasses.

       InputStream socketIn = socket.getInputStream();
       BufferedReader br = new BufferedReader(new InputStreamReader(socketIn));
         while((msg = br.readLine()) != null){
         ...
         }
    
    点赞 评论 复制链接分享

相关推荐