我正在使用以下代码段发出套接字连接
Socket socket = new Socket();
InetSocketAddress endPoint = new InetSocketAddress("localhost", 1234);
try
{
socket.connect(endPoint, 30000);
}
catch (IOException e)
{
e.printStackTrace();
// Logging
}
Run Code Online (Sandbox Code Playgroud)
它尝试连接的端点是脱机的,我想要它做的是尝试连接,并使用30000ms超时,等待结束结果之前的那段时间
目前,30000似乎没有应用该参数,因为从我的日志记录中的时间戳开始,它似乎在1秒内确定连接失败.
如何强制连接在放弃之前等待一段时间?
13:13:57,685 6235 DEBUG [Thread-7] - Unable to connect to [localhost:1234]
13:13:58,685 7235 DEBUG [Thread-7] - Unable to connect to [localhost:1234]
13:13:59,695 8245 DEBUG [Thread-7] - Unable to connect to [localhost:1234]
13:14:00,695 9245 DEBUG [Thread-7] - Unable to connect to [localhost:1234]
Run Code Online (Sandbox Code Playgroud)
编辑:API确实说明Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.但是看起来我没有遇到这样的行为,或者我不服从它,很可能是后者
你在这里得到的是正确的.connect不会坐在套接字上等待它看到服务器,它将尝试连接并等待响应.如果没有要连接的东西,它会返回.如果有连接的东西,它将等待响应的超时秒,如果没有收到则失败.
| 归档时间: |
|
| 查看次数: |
2130 次 |
| 最近记录: |