gpt4 book ai didi

java - 设置单独的 SO_TIMEOUT

转载 作者:行者123 更新时间:2023-12-01 09:55:46 25 4
gpt4 key购买 nike

假设我有多个套接字连接,每个连接可能希望针对 I/O 操作分别在无限超时和有限超时之间切换。 Socket.setSoTimeout() 的文档暗示在任何 Socket 上调用 setSoTimeout() 将更改所有 Socket 的超时code> 对象,因为字段 SocketOptions.SO_TIMEOUT 是静态的。

有没有办法为单个Socket对象设置超时?

最佳答案

不,这并不意味着它将更改所有套接字的超时。它只会更改关联套接字对象的超时。摘自 java 文档 Socket#setSoTimeout

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.

关于java - 设置单独的 SO_TIMEOUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240331/

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