• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Port In Use

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I do not know whether this question should be posted in this section but I feel its a small question.
My problem is that how do I know whether a particular port is already in use. And also what is the range of ports available in Windows or other common OS's.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
range: 1 to 25K on all modern operating systems.
If you want to see if a port is in use, try creating a serversocket on it. If you can't, it's in use.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the range is greater than that; a port number is at least a signed 16-bit integer, so they go up to no less than 32767. i could be wrong about that, too - if port numbers turn out to be unsigned (i'm not sure about that detail), then the numbers would go up to 65535 at a minimum.

many modern OS'es, of course, limit the bottom-most 1024 ports such that ordinary users can't open server sockets on them. but this is seldom much of a concern.

[edit: clarification]
[ May 04, 2005: Message edited by: M Beck ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
So, basically the only way of finding out whether a port is in use is by trying to create a ServerSocket on it and if it throws an exception then it means that the port is in use.
Thank you very much guys.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you try opening up a raw Socket connection to the port to see if it's in use?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, thank you very much for that code, it was excellent.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Glad to help!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, is there any risk in that? I wonder if the server that has a socket open can tell you connected and might do something in response.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic