• Post Reply Bookmark Topic Watch Topic
  • New Topic

port availability  RSS feed

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

When working with a multi-threaded application that involves the use of the
ServerSocket class; the ServerSocket class takes an integer as an arguement
for a Port Number.
i.e. ServerSocket socket = new ServerSocket(2500);

Let's assume this port is already being used by another thread. Is there
some way to check if the port is already active so that we can allocate a
different port number or at least continue to check the port until it
becomes free? I'm thinking maybe using a queue for the port or simply
providing a different port if one is already being used. How does one
generally tackle this problem? How does one query a port?

Alan
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to create a ServerSocket using the proposed port. If you succeed, it was open. If you fail with an exception, it was in use, and you can try another port. That's about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!