windows TCP套接字超时配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/855290/
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-09-15 12:28:19  来源:igfitidea点击:

TCP socket timeout configuration

javawindowssocketstimeoutwindows-server-2008

提问by tuler

I have a Java application which uses a third-party COM component through a Java-COM bridge. This COM component opens a socket connection to a remote host.

我有一个 Java 应用程序,它通过 Java-COM 桥使用第三方 COM 组件。此 COM 组件打开到远程主机的套接字连接。

This host can take some time to respond, and I suspicious that I'm getting a timeout. I'm not sure because as I said, it's a closed-source third-party component.

该主持人可能需要一些时间才能做出响应,而且我怀疑自己超时了。我不确定,因为正如我所说,它是一个封闭源代码的第三方组件。

The API of this component does not expose the socket connection to me, so I have no way to configure the timeout. So I wonder if there is any way to tweak the system default timeout.

这个组件的API没有暴露socket连接给我,所以我没办法配置超时。所以我想知道是否有任何方法可以调整系统默认超时。

I'm using a Windows Server 2008 x64 Enterprise Edition.

我使用的是 Windows Server 2008 x64 企业版。

采纳答案by ShabbyDoo

Do you want to build your application so that it knows at runtime whether or not a timeout happened, or do you want to inspect the behavior of the closed-source COM component? If it's the latter, install Wireshark on your dev box and watch the connection. If it's the former, do you want to ensure that your Java call out to native land doesn't hang forever? If that's the case, look into the java.util.concurrent executor service stuff -- there's a way to call a method in another thread and wait a maximum of N seconds before returning control to your thread.

您想要构建您的应用程序以便它在运行时知道是否发生了超时,还是想要检查闭源 COM 组件的行为?如果是后者,请在您的开发箱上安装 Wireshark 并观察连接。如果是前者,您是否要确保您的 Java 对本土的调用不会永远挂起?如果是这种情况,请查看 java.util.concurrent executor service 的内容——有一种方法可以在另一个线程中调用一个方法,并在将控制权返回给您的线程之前最多等待 N 秒。

回答by Mathew

If you'd like to see the connection establish, and then perhaps drop from timeout, you can use TCPview: http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

如果您想看到连接建立,然后可能从超时中退出,您可以使用 TCPview:http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

回答by Matej

You can introduce your own SocketImplFactory (See static method setSocketImpl()of java.net.Socket class). Then you can create SocketImpl objects with your own value of SO_TIMEOUT parameter.

可以引入自己的SocketImplFactory(参见 java.net.Socket类的静态方法setSocketImpl())。然后您可以使用您自己的 SO_TIMEOUT 参数值创建 SocketImpl 对象。

回答by Pekka

The registry settings described in "Windows Vista Tcp Ip Configuration Settings" at Microsoft for TcpMaxDataRetransmissions may allow you to set the global timeouts you need.

Microsoft 在“Windows Vista Tcp Ip 配置设置”中描述的用于 TcpMaxDataRetransmissions 的注册表设置可能允许您设置所需的全局超时。

You will, of course, need to be careful that you do not cripple the remainder of the server in the process with delayed responses to real errors.

当然,您需要注意不要在此过程中因对实际错误的延迟响应而削弱服务器的其余部分。