File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Port In Use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Port In Use" Watch "Port In Use" New topic
Author

Port In Use

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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.


With Best Regards,
Shyam Prasad Murarka
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: May 02, 2005
Posts: 209
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

Joined: Feb 20, 2001
Posts: 580
Couldn't you try opening up a raw Socket connection to the port to see if it's in use?


James Carman, President<br />Carman Consulting, Inc.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Try this...

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
James, thank you very much for that code, it was excellent.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
No problem. Glad to help!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Port In Use
 
Similar Threads
how to check for open ports of remote computer in java
Mysql jdbc connection takes lot of time
RequestDispatcher
How to block the port
J2ME for socket