read()方法:如果没有可获取的字节,将会阻塞。
源码

   /*** Reads the next byte of data from the input stream. The value byte is* returned as an <code>int</code> in the range <code>0</code> to* <code>255</code>. If no byte is available because the end of the stream* has been reached, the value <code>-1</code> is returned. This method* blocks until input data is available, the end of the stream is detected,* or an exception is thrown.** <p> A subclass must provide an implementation of this method.** @return     the next byte of data, or <code>-1</code> if the end of the*             stream is reached.* @exception  IOException  if an I/O error occurs.*/public abstract int read() throws IOException;available()方法。
/*** 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.** <p> Note that while some implementations of {@code 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.** <p> A subclass' implementation of this method may choose to throw an* {@link IOException} if this input stream has been closed by* invoking the {@link #close()} method.** <p> The {@code available} method for class {@code InputStream} always* returns {@code 0}.** <p> This method should be overridden by subclasses.** @return     an estimate of the number of bytes that can be read (or skipped*             over) from this input stream without blocking or {@code 0} when*             it reaches the end of the input stream.* @exception  IOException if an I/O error occurs.*/public int available() throws IOException {return 0;}

大概意思是:注意,一些实现方法将返回流的全部的字节数,有一些方法则不会,想通过这个方法的返回值来分配缓冲区的长度,来承接流的全部数据,这样是不正确。
尽量不使用available方法,如果使用真的需要使用,在available方法使用之前使用一次read,想下面这样。

//将接收到的数据存到字节数组bytesint firstByte = inputStream.read();int length = inputStream.available();byte[] bytes = new byte[length+1];bytes[0] = (byte)firstByte;inputStream.read(bytes,1,length);

知乎上有一段是这样的。供参考
read的文档说明大致是:如果因已到达流末尾而没有可用的字节,则返回值 -1。在输入数据可用、检测到流的末尾或者抛出异常前,此方法一直阻塞。 socket和文件不一样,从文件中读,读到末尾就到达流的结尾了,所以会返回-1或null,循环结束,但是socket是连接两个主机的桥梁,一端无法知道另一端到底还有没有数据要传输。 socket如果不关闭的话,read之类的阻塞函数会一直等待它发送数据,就是所谓的阻塞。如果发送的东西非常多必须要用循环读的话,可以有一下解决方案:

①在写方调用socket的shutdownOutput方法关闭输出流,该方法的文档说明为,将此套接字的输出流置于“流的末尾”,这样另一端的输入流上的read操作就会返回-1。但是这样不能实现多次交互。
②约定结束标志,当读到该结束标志时退出不再read。
③设置超时,会在设置的超时时间到达后抛出SocketTimeoutException异常而不再阻塞。
④双方定义好通信协议,在协议头部约定好数据的长度。当读取到的长度等于这个长度时就不再继续调用read方法。

作者:qhyuan
链接:https://www.zhihu.com/question/39851782/answer/86588005
来源:知乎

参考连接

socket的read和available()方法相关推荐

  1. confluence中org.apache.tomcat.util.net.NioEndpoint$Acceptor.run Socket accept failed的解决方法

    confluence中org.apache.tomcat.util.net.NioEndpoint$Acceptor.run Socket accept failed的解决方法 参考文章: (1)co ...

  2. php实现返回界面,PHP实现模仿socket请求返回页面的方法

    本文实例讲述了PHP实现模仿socket请求返回页面的方法.分享给大家供大家参考.具体实现方法如下: $url = "www.XXXX.com"; //自己做替换 $parse = ...

  3. C# 网络编程之使用Socket类Send、Receive方法的同步通讯

    经过几天学习,终于解决了再C#网络编程中使用Socket类Send和Receive方法开发的客户端和服务端的同步通讯程序:实现了又客户端想服务器发送消息的界面程序.主要使用的方法是:       1. ...

  4. php socket发数据打印,PHP向socket服务器收发数据的方法

    这篇文章主要介绍了PHP向socket服务器收发数据的方法,分析了socket收发数据的方法,并介绍了socket常用函数,需要的朋友可以参考下 本文实例讲述了PHP向socket服务器收发数据的方法 ...

  5. python socket.error: [Errno 10054] 解决方法

    python socket.error: [Errno 10054] 解决方法 参考文章: (1)python socket.error: [Errno 10054] 解决方法 (2)https:// ...

  6. php中使用Curl、socket、file_get_contents三种方法POST提交数据

    抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...

  7. 在C#中利用Keep-Alive处理Socket网络异常断开的方法

    最近我负责一个IM项目的开发,服务端和客户端采用TCP协议连接.服务端采用C#开发,客户端采用Delphi开发.在服务端开发中我碰到了各种各样的网络异常断开现象.在处理这些异常的时候有了一些心得,现在 ...

  8. curl socket 访问_使用Curl、socket、file_get_contents三种方法POST提交数据 | 学步园

    # <?php # /** # * Socket版本 # * 使用方法: # * $post_string = "app=socket&version=beta"; ...

  9. 实战解读丨Linux下实现高并发socket最大连接数的配置方法

    摘要:Linux操作系统,无论是编写客户端程序还是服务端程序,在高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制. [诉求场景] Linux操作系统,无论是编写 ...

  10. Loadrunner中socket协议十六进制报文参数化方法

    在做tuxedo和socket脚本的过程中,经常会碰到发送的报文是十六进制字符串.而往往我们又需要针对十六进制报文中的某些数据进行参数化.比如pos机交易,银行方面数据交易,几乎使用socket协议. ...

最新文章

  1. 情人节 html5,情人节H5案例 | 2019第一波情人节营销已上线
  2. 基础总结篇之一:Activity生命周期
  3. linux正则表达式_号称“天书”的正则表达式,要这么来讲,我小学三年级已经满分了
  4. 云时代架构阅读笔记十五——架构设计思维(一)
  5. 三、Git多人开发:不同人修改了同文件的不同区域如何处理
  6. C#三层开发做学生管理系统
  7. java获取文件名方法,利用Java获取文件名、类名、方法名和行号的方法小结
  8. python 局域网扫描_Python 简化版扫描局域网存活主机
  9. 四年级信息技术认识计算机,四年级信息技术第三课信息工具知多少
  10. centos7 关闭selinux_Devops之LDAP部署安装(centos7+openLDAP+PhpLDAPAdmin)
  11. 什么是机器学习?有哪些分类?怎样上手开发?终于有人讲明白了
  12. 线性条件随机场代码解读
  13. 2015年12月16日 Oracle语句实现有则更新无则插入
  14. 请MVC5 WebApi2 支持OData协议查询
  15. vuforia的物体识别能识别大物体吗_衢州sensopart 物体识别检测视觉-灵测信息
  16. js小技巧,收藏.作者:空军上将
  17. 禅道 非内置mysql_禅道
  18. Mac 增加国内节假日安排
  19. 通过完全由有理数构成的区间套来揭示无理数的存在
  20. IDEA 出现错误:找不到或无法加载主类

热门文章

  1. mysql sdi_SDI文件扩展名 - 什么是.sdi以及如何打开? - ReviverSoft
  2. Matlab 绘制球面
  3. hiredis中lua脚本调用
  4. 汽车背后那些看不见的软件系统
  5. c# ocx控件安装后不显示问题。
  6. python 描述符参考文档_描述符 - Python 学习笔记 - UDN开源文档
  7. Raspberry-Pi-PICO系列--第八篇 高级篇使用SWD接口下载和调试(第二章)
  8. 无人机设计仿真--在Isight平台上进行的基于CST参数化+Xfoil的无人机翼型优化
  9. .NET WebApi调用微信接口Https请求工具类
  10. ChatGPT自动化