Sockets hanging around

P

paul brown

Hi there,

i've got a simple server app which creates several sockets and (please
believe me!) closes the sockets when appropriate, not leaving anything
hanging around for the GC to close later

i've got loads of unit tests that cause the server to bind its server socket
again and again during the test run

the problem is that sometimes, between tests, the sockets i just closed are
left hanging around

so when a subsequent test runs and i try to bind a server socket to the same
port, i get an exception out of the JVM about not being able to bind an
already bound socket

i'm having to dish out socket number sequentailly for the purposes of
testing, which is a workaround i dont really like

i've done a 'netstat -an' after a test run and i get this:

TCP 127.0.0.1:8888 127.0.0.1:3772 TIME_WAIT
TCP 127.0.0.1:8889 127.0.0.1:3768 TIME_WAIT
TCP 127.0.0.1:8890 127.0.0.1:3767 TIME_WAIT
TCP 127.0.0.1:8892 127.0.0.1:3774 TIME_WAIT
TCP 127.0.0.1:8892 127.0.0.1:3775 TIME_WAIT
TCP 127.0.0.1:8894 127.0.0.1:3773 TIME_WAIT
TCP 127.0.0.1:8895 127.0.0.1:3776 TIME_WAIT
TCP 127.0.0.1:8895 127.0.0.1:3778 TIME_WAIT
TCP 127.0.0.1:8896 127.0.0.1:3777 TIME_WAIT
TCP 127.0.0.1:8905 127.0.0.1:3785 TIME_WAIT
TCP 127.0.0.1:8907 127.0.0.1:3786 TIME_WAIT

these are my sockets ... how come they are still listed here dozens of
seconds after i've close()'d all my sockets?

(as you can see, i vary the server socket port used between tests in order
to avoid the problem i describe...hence
all the 88xx and 89yy port numbers in this display)

Thanks,
Paul

ps. win2K, jdk 1.4.2_02
 
T

Tony Morris

paul brown said:
Hi there,

i've got a simple server app which creates several sockets and (please
believe me!) closes the sockets when appropriate, not leaving anything
hanging around for the GC to close later

i've got loads of unit tests that cause the server to bind its server socket
again and again during the test run

the problem is that sometimes, between tests, the sockets i just closed are
left hanging around

so when a subsequent test runs and i try to bind a server socket to the same
port, i get an exception out of the JVM about not being able to bind an
already bound socket

i'm having to dish out socket number sequentailly for the purposes of
testing, which is a workaround i dont really like

i've done a 'netstat -an' after a test run and i get this:

TCP 127.0.0.1:8888 127.0.0.1:3772 TIME_WAIT
TCP 127.0.0.1:8889 127.0.0.1:3768 TIME_WAIT
TCP 127.0.0.1:8890 127.0.0.1:3767 TIME_WAIT
TCP 127.0.0.1:8892 127.0.0.1:3774 TIME_WAIT
TCP 127.0.0.1:8892 127.0.0.1:3775 TIME_WAIT
TCP 127.0.0.1:8894 127.0.0.1:3773 TIME_WAIT
TCP 127.0.0.1:8895 127.0.0.1:3776 TIME_WAIT
TCP 127.0.0.1:8895 127.0.0.1:3778 TIME_WAIT
TCP 127.0.0.1:8896 127.0.0.1:3777 TIME_WAIT
TCP 127.0.0.1:8905 127.0.0.1:3785 TIME_WAIT
TCP 127.0.0.1:8907 127.0.0.1:3786 TIME_WAIT

these are my sockets ... how come they are still listed here dozens of
seconds after i've close()'d all my sockets?

(as you can see, i vary the server socket port used between tests in order
to avoid the problem i describe...hence
all the 88xx and 89yy port numbers in this display)

Thanks,
Paul

ps. win2K, jdk 1.4.2_02

You clearly have client connections open to those sockets.
All of them are from your local machine and the client port is 37xx

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
T

Thomas Schodt

paul said:
sometimes, between tests, the sockets i just closed are
left hanging around ....
so when a subsequent test runs and i try to bind a server socket to the same
port, i get an exception out of the JVM about not being able to bind an
already bound socket ....
these are my sockets ... how come they are still listed here dozens of
seconds after i've close()'d all my sockets?

The Java doc on Socket.setReuseAddress() should give you some hints.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top