Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Port busy: netstat vs ServerSocket

807607Jan 26 2007 — edited Jan 26 2007
I need to check whether a port is busy or not, and I get strange results when comparing the results of making a check with netstat (Windows) and with server socket binding to an address. My check is in a bat file that calls netstat, calls a java program that checks with sockets, then netstat again. Below are three outputs. One is received while the port was really busy, the second is a little time after it should have beeen freed, third is long after the port was freed.

What worries me is the second test, where netstat says there is a process listeining on the port, then my program says it is free, then netstat says the other process is still listening.

Is there some period when the port can be in some undetermined state when it is possible to both establish a server socket on it and it still be in listening state from the point of view of the previous owner?
The problem is that it is not my application that I need the port for, I need to obtain a free port to give start another application on and then connect from my application to that other app.
[1]
F:\temp>r
netstat -an | find "8100"
  TCP    127.0.0.1:3438         127.0.0.1:8100         TIME_WAIT
  TCP    127.0.0.1:3465         127.0.0.1:8100         ESTABLISHED
  TCP    127.0.0.1:8100         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:8100         127.0.0.1:3465         ESTABLISHED
java A 8100
Cannot bind:0.0.0.0:8100
Cannot bind:127.0.0.1:8100
Cannot bind:localhost:8100
netstat -an | find "8100"
  TCP    127.0.0.1:3438         127.0.0.1:8100         TIME_WAIT
  TCP    127.0.0.1:3465         127.0.0.1:8100         ESTABLISHED
  TCP    127.0.0.1:8100         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:8100         127.0.0.1:3465         ESTABLISHED

[2]
F:\temp>r
netstat -an | find "8100"
  TCP    127.0.0.1:3438         127.0.0.1:8100         TIME_WAIT
  TCP    127.0.0.1:3465         127.0.0.1:8100         ESTABLISHED
  TCP    127.0.0.1:8100         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:8100         127.0.0.1:3465         ESTABLISHED
java A 8100
Bound:0.0.0.0:8100
Bound:127.0.0.1:8100
Bound:localhost:8100
netstat -an | find "8100"
  TCP    127.0.0.1:3438         127.0.0.1:8100         TIME_WAIT
  TCP    127.0.0.1:3465         127.0.0.1:8100         ESTABLISHED
  TCP    127.0.0.1:8100         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:8100         127.0.0.1:3465         ESTABLISHED


[3]
F:\temp>r
netstat -an | find "8100"
java A 8100
Bound:0.0.0.0:8100
Bound:127.0.0.1:8100
Bound:localhost:8100
netstat -an | find "8100"
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2007
Added on Jan 26 2007
5 comments
926 views