What is the timeout for a socket connection created using a connector constructor?

What is the timeout for a socket connection created using a connector constructor?

In Java SE 6, the following constructors for Socket will immediately connect the socket instead of calling it after the build:

  • Socket(InetAddress address, int port)
  • Socket(InetAddress host, int port, boolean stream)
  • Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
  • Socket(String host, int port)
  • Socket(String host, int port, boolean stream)
  • Socket(String host, int port, InetAddress localAddr, int localPort)

Although it’s nice and convenient, all that Java SE developers have created 500 ways to build a socket so that you can just look at a list of 500 to find the one that does what you want (instead of calling new Socket() on Socket#connect() ), none of the documents of these designers say what the connection timeout is or what they call connect(SocketAddress endpoint, int timeout) .

Perhaps the material in the docs constructor talking about createSocketImpl implies something about a timeout, or do some documents elsewhere say?

Does anyone know what the actual connection timeout is for any of these constructors?


Background: Well, assuming the spec is really ambiguous (did I think Java is portable?), I'm trying to understand why the client code freezes at seemingly random times. I have code that calls some open source library that calls one of these constructors. I want to know if a call to one of these constructors could make the timeout endless or very long. I don’t know which version of the JDK the client is using, so it would be nice if the spec talked about a timeout somewhere. I think I can probably get the JDK version from my client, but it will probably be a private JDK. In this case, could I deploy the code in my version of the SE library to find out? It's complicated? Will I go to jail?

+4
source share
6 answers

The Java specification is dummy. It does not say what timeout is on any of these constructors, so the implementation can set the timeout to 0.000000000001 nanoseconds and still be true. Furthurmore: not a finite timeout not even respected by vm implementations (as seen here), so it seems the specification doesn't even matter because no one followed it.

Conclusion: you need to read the binary code of the binary code of the JVM client (possibly illegal, but you have to do what you need), as well as the OS socket socket.

+3
source

Although the Java docs state that the timeout is infinite, this actually means that the JVM will not impose any timeout on the connection operation, but the OS can impose timeout settings on any socket operations.

Thus, the actual timeout will depend on the settings of your TCP / IP level of your OS.

It is good programming practice to set timeouts for all socket operations, preferably configured through a configuration file. The advantage of configuring it is that depending on the network load of the deployment environment, the timeout can be changed without re-creating / re-testing / re-releasing all the software.

+3
source

Looking at the code of Socket in OpenJDK 6-b14 , you can see that these constructors are called connect(socketAddress, 0) , which means an infinite timeout value.

+2
source

According to the sources (I look here 1.5_13, but there shouldn't be a difference), different Socket constructors all call Socket(SocketAddress, SocketAddress, boolean) , which is defined as:

 private Socket(SocketAddress address, SocketAddress localAddr, boolean stream) throws IOException { setImpl(); // backward compatibility if (address == null) throw new NullPointerException(); try { createImpl(stream); if (localAddr == null) localAddr = new InetSocketAddress(0); bind(localAddr); if (address != null) connect(address); } catch (IOException e) { close(); throw e; } } 

connect(SocketAddress) is defined as

 public void connect(SocketAddress endpoint) throws IOException { connect(endpoint, 0); } 

Therefore, an infinite timeout (as @Keppil already pointed out).

+2
source

The Socket class exists since Java 1.0, but at that time it was possible to create sockets that were immediately connected, and it was not possible to specify a connection timeout. Starting with Java 1.4, it was possible to create unconnected sockets and then specify a timeout using the connect method. I assume that someone simply forgot to clarify the documentation of the "old" constructors, indicating that they still work without an explicit timeout.

The documentation of connection methods with a timeout parameter states that "a zero timeout is interpreted as an infinite timeout." This is also not true, since it means that the Java VM does not require a timeout. Even with a timeout of 0, the connection operation may still be delayed in the TCP / IP stack of the operating system.

+1
source

It depends on the platform, but in about a minute. Javadoc for connect () is incorrect, indicating that it is infinite. Also note that the timeout connect () parameter can only be used to decrease the default value, and not to increase it.

+1
source

Source: https://habr.com/ru/post/1440801/