gpt4 book ai didi

java - setSoTimeout 的功能是什么以及它是如何工作的?

转载 作者:IT老高 更新时间:2023-10-28 20:37:44 29 4
gpt4 key购买 nike

我正在尝试自己学习 Socket。我对 Oracle 网站上的以下文字有点困惑。我对此有一些疑问。提前感谢您提供任何明确的解释。

setSoTimeout

public void setSoTimeout(int timeout) throws SocketException

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 是连接的端点。如果我说

    mySocket.setSoTimeout(2000);

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

  3. 什么意思timeout expire

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

  5. Infinite Timeout表示socket不再读取?

最佳答案

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?

不,表示如果2000ms内没有数据到达,会抛出SocketTimeoutException

What does it mean timeout expire?

这意味着 2000 毫秒(在您的情况下)过去了,没有任何数据到达。

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

没有一个“必须”启用。如果您的意思是“可能启用”,这就是其中之一。

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

多么奇怪的建议。这意味着如果没有数据到达,您将永远阻塞读取。

关于java - setSoTimeout 的功能是什么以及它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12820874/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com