Reputation: 35
I set the timeout on the socket, which is valid if the value is less than 21 seconds, and after 21 seconds, I find that the timeout is still 21 seconds
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String time1= sdf.format(new Date());
System.out.println(time1);
try {
Socket sock = new Socket();
SocketAddress socketAddress = new InetSocketAddress("128.28.28.28",80);
sock.connect(socketAddress,60000);
}catch (Exception ex){
ex.printStackTrace();
}finally {
time1= sdf.format(new Date());
System.out.println(time1);
}
}
Running this code should take 60 seconds to time out, but it does time out after about 21 seconds.But if you change that 60,000 to 5,000, you'll see that it's actually over in five seconds.I suspect the socket's default timeout is only 21 seconds, but I can't find evidence.I want to find the evidence and know what I should do to get him to go 60 seconds over instead of 21. this host: 128.28.28.28 is a non-existent address.My question is why I set the timeout to 60 sconds, but it was timed out in 21 seconds.
Error:
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.dongbawen.hppa.biz.rulemstrecipe.Send_Class.main(Send_Class.java:49)
Upvotes: 2
Views: 1823
Reputation: 10020
The socket call is, at the lowest level, handled by your operating system (OS), which may impose restrictions upon the allowed values of parameters such as timeouts. See for example: Overriding the default Linux kernel 20-second TCP socket connect timeout. So, regardless of what value you set in application code, the OS may be lowering it to the highest value it actually allows. The value of about 21 seconds which you experimentally see may be the actual limit set in your OS, or, more likely, the value may be set to 20 seconds, but since most timeouts are not real-time values but only handled on best-effort basis, the actual value you observe may be slightly above the nominal 20 seconds.
Upvotes: 1