setSoTimeout 的功能是什么以及它是如何工作的? [英] What is the functionality of setSoTimeout and how it works?

查看:30
本文介绍了setSoTimeout 的功能是什么以及它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自己学习 Socket.我对 Oracle 网站上的以下文字感到有些困惑.我对此有一些疑问.在此先感谢您的明确解释.

I'm trying to learn Socket by myself. I'm little bit confused by following text from Oracle website. I have some questions regarding that. Thanks in advance for any clear explanation.

setSoTimeout

public void setSoTimeout(int timeout) 抛出 SocketException

public void setSoTimeout(int timeout) throws SocketException

使用指定的超时时间(以毫秒为单位)启用/禁用 SO_TIMEOUT.将此选项设置为非零超时后,与此 Socket 关联的 InputStream 上的 read() 调用将仅阻塞这段时间.如果超时到期,则会引发 java.net.SocketTimeoutException,尽管 Socket 仍然有效.必须在进入阻止操作之前启用该选项才能生效.超时必须 > 0.超时为零被解释为无限超时.

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

  1. 什么是 SO_TIMEOUT?
  2. Socket 是连接的端点.如果我说

  1. What is SO_TIMEOUT?
  2. Socket is the endpoint of a connection. If I say

mySocket.setSoTimeout(2000);

这是否意味着我在 2000 毫秒内阻止从服务器/客户端读取此套接字的任何输入,此后套接字已准备好读取数据?

Does it mean that I'm blocking reading any input from the Server/Client for this socket for 2000 millisecond and after this time the socket is ready to read data?

超时到期是什么意思?

在阻止操作之前必须启用的选项是什么?

What is the option which must be enabled prior to blocking operation?

无限超时意味着套接字不再读取?

Infinite Timeout means that the socket does't read anymore?

推荐答案

JavaDoc 解释得很好:

将此选项设置为非零超时后,对与此 Socket 关联的 InputStream 将仅为此阻塞多少时间.如果超时到期,则java.net.SocketTimeoutException 被引发,尽管 Socket 仍然是有效的.该选项必须在进入阻止之前启用操作才能生效.超时必须 > 0.超时为零被解释为无限超时.

With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

SO_TIMEOUTread() 调用将阻塞的超时时间.如果达到超时,java.net.SocketTimeoutException 将被抛出.如果你想永远阻塞,把这个选项设置为零(默认值),那么 read() 调用将阻塞,直到至少可以读取 1 个字节.

SO_TIMEOUT is the timeout that a read() call will block. If the timeout is reached, a java.net.SocketTimeoutException will be thrown. If you want to block forever put this option to zero (the default value), then the read() call will block until at least 1 byte could be read.

这篇关于setSoTimeout 的功能是什么以及它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆