gpt4 book ai didi

Java getInputStream SocketTimeoutException 而不是 NoRouteToHostException

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:56 26 4
gpt4 key购买 nike

尝试在 Linux (RHEL) 上打开多个输入流(在单独的线程中)时,我遇到了一个奇怪的问题。该行为在 Windows 上按预期工作。

我正在启动 3 个线程以打开到 3 个不同服务器的 https 连接。这三个都是无效的 IP 地址(在此测试用例中),因此我希望它们中的每一个都出现 NoRouteToHostException。前两个按预期返回这些,而且很快。 (请参阅下面的堆栈跟踪)但是第三个(当我以这种方式测试时是第四个)没有给出无路由异常。他们等待很长时间,然后给出 SocketTimeoutException(请参阅下面的其他堆栈跟踪)。这需要很长时间才能返回,并且不能准确表达连接问题。

有问题的代码行是:

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

有没有人见过这样的东西? REHL 上的套接字是否存在多线程问题,或者在某个地方限制了一次可以连接的数量……或者……什么的?

预期的堆栈跟踪,如前两个收到的那样:

java.net.NoRouteToHostException: No route to host        at java.net.PlainSocketImpl.socketConnect(Native Method)        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)        at java.net.Socket.connect(Socket.java:529)        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)        at sun.net.NetworkClient.doConnect(NetworkClient.java:158)        at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)        at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)        at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:272)        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177)        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)

3 日收到的意外堆栈跟踪:

java.net.SocketTimeoutException: connect timed out        at java.net.PlainSocketImpl.socketConnect(Native Method)        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)        at java.net.Socket.connect(Socket.java:529)        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)        at sun.net.NetworkClient.doConnect(NetworkClient.java:158)        at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)        at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)        at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:272)        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177)        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)

最佳答案

Linux 防火墙是否阻止了连接?如果 iptables -L 的输出已填充,其中可能包含您的答案。您也可以使用 telnet localhost <portnumber> 进行测试检查端口是否可用。如果是,telnet 应该显示“已连接”等。

关于Java getInputStream SocketTimeoutException 而不是 NoRouteToHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617965/

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