Java 如何在 Linux 上查看/更改套接字连接超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1045964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:42:58  来源:igfitidea点击:

How to view/change socket connection timeout on Linux?

javalinuxsocketstimeoutconnect

提问by Kevin

When creating a Socket in Java:

在 Java 中创建 Socket 时:

new Socket(host, port);

The Socket constructor will try to connect to host:portbefore returning. On Windows, this fails almost immediately for unreachable hosts but for Linux it can take up to 5 minutes for the Socket to timeout.

Socket 构造函数将在返回之前尝试连接到host:port。在 Windows 上,对于无法访问的主机,这几乎立即失败,但对于 Linux,Socket 可能需要长达 5 分钟才能超时。

I'm aware that if I have control over creating the Sockets, I can do:

我知道如果我可以控制创建套接字,我可以这样做:

Socket s = new Socket();
s.bind(..);
s.connect(.., timeout);

but I'd rather have the OS use a reasonable default value. Is there a way to change this setting on Linux?

但我宁愿让操作系统使用合理的默认值。有没有办法在 Linux 上更改此设置?

Thanks

谢谢

采纳答案by Duck

I think you want /proc/sys/net/ipv4/tcp_syn_retries. The default is usually 5 or 6 which comes out to around 3 minutes.

我想你想要/proc/sys/net/ipv4/tcp_syn_retries。默认值通常是 5 或 6,大约 3 分钟。

Note that these are system-wide.

请注意,这些是系统范围的。

回答by DreadPirateShawn

It's my understanding that this depends on the system's TCP/IP default timeout (240 seconds by default?)... one option is to try tweaking those, however this could affect any other programs on the same machine which rely on the timeout value. In which case, it might be safer to simply lower the "timeout" value in your Java connect() call, instead.

我的理解是这取决于系统的 TCP/IP 默认超时(默认为 24​​0 秒?)...一种选择是尝试调整这些,但这可能会影响同一台机器上依赖超时值的任何其他程序。在这种情况下,简单地降低 Java connect() 调用中的“超时”值可能更安全。

回答by akarnokd

I would advise against changing OS settings as it might affect other applications unexpectedly. The Socket.setSoTimeout()method might help you too.

我建议不要更改操作系统设置,因为它可能会意外影响其他应用程序。该Socket.setSoTimeout()方法也可能对您有所帮助。

回答by eckes

It is BTW not entirely correct, that Linux and Windows behave here differently. Besides the initial SYN retries (which can be configured on Linux and Windows) the neighbour state as well as other routers sending RST packets also play a role.

顺便说一句,Linux 和 Windows 在这里的行为不同,这并不完全正确。除了初始 SYN 重试(可以在 Linux 和 Windows 上配置)之外,邻居状态以及发送 RST 数据包的其他路由器也起作用。

If a connection attempt on Windows fails immediatelly it is likely that it was eighter RSTed by a router or the neighbour was recognized as unreachable on ARP level. Try the arp -a -vcommand on Windows to see the unreachable hosts - which get rejected quickly.

如果 Windows 上的连接尝试立即失败,则很可能是路由器对其进行了 8 次 RST,或者邻居在 ARP 级别被识别为无法访问。arp -a -v在 Windows 上尝试该命令以查看无法访问的主机 - 很快就会被拒绝。

For Linux you would use ip neighto list the reachability state of stations on your local network.

对于 Linux,您可以ip neigh用来列出本地网络上站点的可达性状态。