readInt函数的使用

今天看人家java网络编程源码的时候,看到readInt函数。由于自己对java不是很熟悉,虽然从函数字面意思大概知道函数的用法,还是进一步查阅了java api文档。下面内容是从java api文档中摘抄的:

readInt

public final int readInt()
                  throws IOException
See the general contract of the  readInt method of  DataInput.

Bytes for this operation are read from the contained input stream.

Specified by:
readInt in interface  DataInput
Returns:
the next four bytes of this input stream, interpreted as an  int.
Throws:
EOFException - if this input stream reaches the end before reading four bytes.
IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
See Also:
FilterInputStream.in

注意标红的部分,该函数的意思就是从输入流中读取四个字节。在网络编程中,经常需要构造自己的包结构,最简单的就是用最开始的4bit存储后续data部分的长度。这样在java网络编程中,首先就可以利用readInt函数来读取data部分的长度,然后在利用recv函数循环接受数据内容时,利用该长度值作为判断条件,从而正确接收网络数据。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值