java SocketTimeoutException:接受超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4543858/
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
SocketTimeoutException: Accept timed out
提问by user555641
The following bit of code throws java.net.SocketTimeoutException: Accept timed out
:
以下代码抛出java.net.SocketTimeoutException: Accept timed out
:
ServerSocket serverSocket = new ServerSocket(0, 1, InetAddress.getLocalHost());
serverSocket.setSoTimeout(6000);
serverSocket.accept();
I have tried changing everything I can in creating a ServerSocket
but the error remains the same. Please guide me in what I'm missing here, if anything.
我尝试在创建 a 时更改我所能做的一切,ServerSocket
但错误仍然相同。如果有的话,请指导我在这里缺少什么。
回答by Stephen C
What your code is doing is listening for 6 seconds for incoming TCP/IP requests on port zero for the local host1.
您的代码正在为本地主机1 的端口 0 上的传入 TCP/IP 请求监听 6 秒。
Here are some reasons why you might get a SocketTimeoutException
.
以下是您可能会获得SocketTimeoutException
.
- Nothing tries to connect to your service within the 6 second timeframe.
- Something tries to connect, but it is trying to connect on the wrong port. (Port zero sounds to me like you are trying to accept requests on "any" port, and I think that is unlikely to work.)
- There is a software or hardware firewall (or packet filter) that is preventing connection requests from reaching your application, or is blocking the replies.
- 在 6 秒的时间范围内没有任何尝试连接到您的服务。
- 有些东西试图连接,但它试图连接到错误的端口。(在我看来,零端口听起来像是您试图接受“任何”端口上的请求,我认为这不太可能奏效。)
- 有软件或硬件防火墙(或数据包过滤器)阻止连接请求到达您的应用程序,或阻止回复。
1 - If you don't want that "only accept an exception if it arrives within 6 seconds" behaviour ... which strikes me as a bit odd ... you shouldn't set a timeout on the server socket object.
1 - 如果您不希望“仅接受在 6 秒内到达的异常”行为……这让我觉得有点奇怪……您不应该在服务器套接字对象上设置超时。