user590444
user590444

Reputation: 4332

How to override system default TCP timeout

I am using old libraries based on JDK 1.4 and I found that there is a huge timeout for attempts to connect to unexisting urls. Investigation revealed next problem:

Prior to JDK 1.5 there was no setting for the connection timeout on a URLConnection object. Instead it would simply use the default OS level timeout value for TCP timeout (which by default happens to be 1800 seconds/30 minutes). JDK1.5 and after included the option to set a custom timeout that overrides the default setting provided by the operating system.

Is it possible to override 'TCP timeout OS setting' from java so code written on java 1.4 use this settings?

Upvotes: 0

Views: 1971

Answers (3)

Kristian Evensen
Kristian Evensen

Reputation: 1345

If you have access to the socket object(s), you can change the timeout by setting the SO_TIMEOUT socket option. This allows you to change the timeout for all blocking calls (for example connect()). It is available in 1.4.2 at least.

Upvotes: 0

user207421
user207421

Reputation: 310988

Instead it would simply use the default OS level timeout value for TCP timeout (which by default happens to be 1800 seconds/30 minutes)

That's not true. There are several TCP timeouts, but the one that applies here is the connect timeout, which consists of three attempts with internal timeouts like 10, 20, 40 seconds, total 70 seconds. Certainly not half an hour. In fact, I'm not aware of any TCP timeout that defaults to half an hour.

Is it possible to override 'TCP timeout OS setting' from java so code written on java 1.4 use this settings?

You could run regedit or whatever is appropriate on the target system, but you'll be changing it for everybody, which is certainly not advisable.

However, given the mistake above, I am wondering whether you really have a problem at all. You should be able to live with a one-minute default connect timeout.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533730

You need to call the system command which would change this on your machine

  • you need to be root or have sudo access
  • it changes it for every program
  • you need to know what it is for each platform you support.

Or you could upgrade to Java 7. Even Java 6 is end of line.

Upvotes: 2

Related Questions