英文:
Exception "java.net.ConnectException: Connection timed out: connect" is thrown before specified timeout
问题
以下是翻译好的代码部分:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ScoketTimeoutMain
{
public static void main( String[] args )
{
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress( "example.com", 81 );
final long l = System.currentTimeMillis();
try
{
socket.connect( address, 60000 );
}
catch( IOException aE )
{
aE.printStackTrace();
System.out.println( System.currentTimeMillis() - l );
}
}
}
在约21秒后出现连接超时异常:
java.net.ConnectException: 连接超时: 连接
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.kulig.ScoketTimeoutMain.main(ScoketTimeoutMain.java:17)
21004
我不明白为什么在约21秒后会抛出异常,而不是60秒。
===编辑===
如果我将超时时间设置得小于21秒,那么它会正常工作。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ScoketTimeoutMain
{
public static void main( String[] args )
{
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress( "example.com", 81 );
final long l = System.currentTimeMillis();
try
{
socket.connect( address, 2000 );
}
catch( IOException aE )
{
aE.printStackTrace();
System.out.println( System.currentTimeMillis() - l );
}
}
}
在约2秒后出现连接超时异常:
java.net.SocketTimeoutException: 连接超时
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.kulig.ScoketTimeoutMain.main(ScoketTimeoutMain.java:17)
2002
所以,似乎发生了另一个超时。我不知道是什么原因导致的,也不知道如何设置这个其他的超时时间。
英文:
I have following code:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ScoketTimeoutMain
{
public static void main( String[] args )
{
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress( "example.com", 81 );
final long l = System.currentTimeMillis();
try
{
socket.connect( address, 60000 );
}
catch( IOException aE )
{
aE.printStackTrace();
System.out.println( System.currentTimeMillis() - l );
}
}
}
Connection timeout is thrown after about 21 seconds:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.kulig.ScoketTimeoutMain.main(ScoketTimeoutMain.java:17)
21004
I do not understand why exception is thrown after 21 seconds instead of 60 seconds.
===EDIT===
If i set timeout smaller than 21 seconds then it works properly.
E.g.:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ScoketTimeoutMain
{
public static void main( String[] args )
{
Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress( "example.com", 81 );
final long l = System.currentTimeMillis();
try
{
socket.connect( address, 2000 );
}
catch( IOException aE )
{
aE.printStackTrace();
System.out.println( System.currentTimeMillis() - l );
}
}
}
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.kulig.ScoketTimeoutMain.main(ScoketTimeoutMain.java:17)
2002
So some another timeout occurs. I have no idea what can cause it and how to set this other timeout.
答案1
得分: 2
我得到了完全相同的21秒(21007)。在互联网上搜索,21秒的超时并不罕见。根据这个热补丁,我修改了TCP的“Max SYN Retransmissions”参数,将超时时间改为了60秒:
netsh interface tcp set global MaxSynRetransmissions=5
默认值是2
。
英文:
I got the exact same 21 seconds (21007). Searching the internet, the 21 seconds timeout is not that rare. Based on this hotfix, I changed the Max SYN Retransmissions
parameter of TCP and got the 60 seconds timeout:
netsh interface tcp set global MaxSynRetransmissions=5
Default value is 2
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论