Skip to main content

How do I find if a particular port is free on the local machine

1 reply [Last post]
Joined: 2008-09-09

I would like to find from a Java program if a particular port of the machine on which the program runs is free. I DO NOT want to bind to that port. I just want to identify if the port is free.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-11-12

You can try a "brute force" method listed at

Essentially it just loops through all the port numbers and executes the following code:

[i]try {
Socket theSocket = new Socket(host, i);
System.out.println("There is a server on port " + i + " of " + host);

I think technically you are not binding the port numbers this way since you close the socket as soon as you create it.

Hope this makes sense.