1

We have an issue on one of customers servers, where something seems to close the java application HTTP socket, and not let it open afterwards for some time.

Meaning it goes like this: 1) Application works fine, then something causing the socket to close. 2) Any subsequent attempts to open it, including application restart will produce the "java.net.BindException: Address already in use" for some time. 3) Then it would finally let open the socket via another application restart.

It's the first time we see such issue happening, and quite stumbled by it.

Does it rings a bell for anyone?

OS: 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64, CentOS release 5.5 Java version: 1.6.0_20

Thanks!

SyBer
  • 5,407
  • 13
  • 55
  • 64

4 Answers4

2

Seems like you should tell your Linux to create socket with immediate rebinding allowed, see SO_REUSEADDR in man 7 socket.

Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51
  • Gah, actually it seem to be due to "Too many open files" issue that happens on our side after all - Java behaves erratically after such exception. Added the SO_REUSEADDR anyhow to prevent potential issues in the future. – SyBer Nov 16 '10 at 11:38
0

Sounds like you should be investigating the 'something causing the socket to close' part.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

As for what's closing the socket, you'll have to investigate your code. It's not something external that closes the socket, it can only be your code.

The behaviour you see when you attempt to bind to that socket again is normal and expected, and there's some explanations as for why here.

You can set the SO_REUSEADDR socket option to tell the system to go ahead and allow a program to bind to that port anyway. For java, see here

Community
  • 1
  • 1
nos
  • 223,662
  • 58
  • 417
  • 506
-1

You can't do an instant rebind as sockets linger until all queued messages for the socket have been successfully sent or the linger timeout has been reached. You can change this policy with SO_LINGER.

More info can be found here (manpage) and here (javadoc)

As for the closing problem, this seems like a bug in your code.

mcabral
  • 3,524
  • 1
  • 25
  • 42
  • Ports linger, not sockets. You shouldn't fiddle with SO_LINGER. SO_REUSEADDR might be what he needs: more probably, a solution to the *original* problem of the unexpected exit. – user207421 Nov 13 '10 at 09:18
  • In fact you are mis-stating the position on 'linger' here. 'Linger' during `close()` only happens if you've set a positive SO_LINGER timeout: it blocks while pending data is sent from the socket send buffer. By default `close()` is asynchronous. – user207421 Nov 04 '16 at 00:10