Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to find a port is free or not without catching any Exception ?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Im making use of the below code snippet for finding whether a port is free or not

try {
InetSocketAddress addr = new InetSocketAddress(InetAddress.getByName("hostname"),1529); // initialization not shown
Socket s = new Socket();
s.connect(addr);
s.close();
} catch (IOException e) {
canConnect = false;
e.printStackTrace();
}

Is there way from which I can find out whether a port is free or not without getting any exception as in above code .
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venki,
If you want to connect to any free port then just do the following.

Socket socket = new Socket("localhost",0);

In port, you have to insert as port number
 
venki sheshgiri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunil
The actual requirement is to find a list of free ports b/n a range say between 7000 to 9000 we will just find a port say 7002 which is free then incrementally use other ports which are free untill the range 9000 to host our services.
This has to work across all OS (HP ,IBM AIX,Sun Solaris,Windows)

didnt get "In port, you have to insert as port number" where I have to insert ?

Regards
Venki
 
sunil sahoo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venki,
I think you have first check for each operating system and then you have to run operating system specific commands to get the used ports
example
Below is the command to find out used ports for linux
netstat -tulnap
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic