• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how to find out available ports

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I want to find out the number of available ports through java.How can I do this?

with regards,
ajse
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try binding to them. Really there is no API call for this, and there should not be. Besides, attempting to bind a socket to an IP/Port combination takes about 1 millisecond, so choose a range of ports attempt to bind to them. But remember, with this method, and any hypothetical "free ports" method, just because a port is "free" this millisecond, does not mean it will be free in the next millisecond (this is especially true for ports above 20,000).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sample Source Code:

Like what was earlier said, I port is free now does not mean that it will be free later. A port is blocked now does not mean that it will not be free later.

import java.net.*;
import java.io.*;

public class Test {

public static final String NEWLINE = System.getProperty( "line.separator" );

public void tryToConnect(InetAddress address, int port ) {

try
{
Socket socket = new Socket( address, port );
socket.close();

System.out.println( "Connected successfully to " + address + " : " + port );
}
catch ( Exception ex )
{

System.out.println( "Error attempting to connect to " + address + " : " + port + " with exception : " + ex.getMessage() );
}

}


public void tryToConnect(String host, int port ) {

try
{

InetAddress address = InetAddress.getByName( host );
Socket socket = new Socket( address, port );
socket.close();

System.out.println( "Connected successfully to " + host + " : " + port );
}
catch ( Exception ex )
{

System.out.println( "Error attempting to connect to " + host + " : " + port + " with exception : " + ex.getMessage() );
}

}

public static void main( String[] args ) throws Exception {

System.out.println( "Starting the test" );

Test test = new Test();

test.tryToConnect( "localhost", 80 );

System.out.println( "Done with the test" );
}
}
 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another sample

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic