gpt4 book ai didi

java - Socket setSoTimeout 说明

转载 作者:行者123 更新时间:2023-11-29 07:13:11 26 4
gpt4 key购买 nike

我有一个场景,其中有服务器监听指定的 ip 和端口以及连接到该服务器的客户端。现在我正在使用 readline 方法从服务器读取响应:

String readme=bs.readline()).   

这里的bsbufferedreader对象。我想知道在阅读回复之前是否写了这一行

socket.setSoTimeout(1000)

如果直到 1000 毫秒都没有响应

无论套接字超时并断开连接还是不断开套接字并在自述文件中给出空字符串。

最佳答案

其实都不是。抛出 SocketTimeoutException

来自docs :

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.

Parameters: timeout - the specified timeout, in milliseconds. Throws: SocketException - if there is an error in the underlying protocol, such as a TCP error.

关于java - Socket setSoTimeout 说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885909/

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