Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Find PID from port  RSS feed

 
Nikolay Tsonkov
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a program that checks available port numbers. Sometimes it reports that certain ports are not available but I cannot find what application is using them - I've tried netstat -aon. For the program I'm using:



What I need to know is if there is a way to check what the PID of the application using that port is.
Any suggestions or advises would be appreciated.

 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try CurrPorts. It's a little freeware utility that does just what you want - show all open ports and the applications these ports belong to.
 
Nikolay Tsonkov
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,

But I want to this with Java code if it is possible at all. I want to get the pid of the application that is listening to a reserved port automatically without using external utilities or console commands with:



 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you will need to use JNI. This information is simply not available from pure Java.

There are two possible ways to do this, all depending on what functions the Windows API provides:

1)
- using the port, directly find the ID of the process that has the port open. I don't know if this is possible
- get the details of this process

2)
- iterate through all processes (create a snapshot using CreateToolhelp32Snapshot, then use Process32First and Process32Next to iterate)
- for the current process find all open ports (should be possible, don't know how to...)

For more information on the native calls I recommend you go to Microsoft's MSDN site.
 
him jain
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can add a little help to you.
You can see which ports are open by checking this link:
http://jainhim.blogspot.com/2010/04/which-ports-are-open.html

But I don't know whether you will be able to invoke it in your java program or not :P
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input, but except for the first command these do not work on Windows. The first one does not show the processes that these ports belong to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!