我试图连接到套接字socket = new Socket(server , port);
,但是如果服务器不可用,我需要设置连接失败的超时时间。默认超时为3分钟,例如,如果我在11:50:22开始连接,则错误只会在11:53:31出现。查看日志:
12-03 11:50:22.519: E/InternetIntentService(23897): Start connecting to localhost:9999
12-03 11:53:31.869: W/System.err(23897): java.net.ConnectException: failed to connect to /localhost (port 9999): connect failed: ETIMEDOUT (Connection timed out)
如何将此3分钟更改为10秒?
Socket
使用no-args构造函数创建一个,然后调用connect(endpoint, timeout);
:
Socket s = new Socket();
s.connect(endpoint, timeout);
请注意,与Javadoc相反,零并不意味着无穷大,它意味着平台默认值(大约一分钟),并且您只能使用此技术来降低平台默认值,而不能提高平台默认值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句