aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes how to check whether port is available on remote pc using tcp and udp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "how to check whether port is available on remote pc using tcp and udp" Watch "how to check whether port is available on remote pc using tcp and udp" New topic
Author

how to check whether port is available on remote pc using tcp and udp

James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
How to check whether port is in use on remote pc?

When I want to check it using TCP I assume I can do it like this

and if it throws exception - it is in use.

But how to do the same thing but using UDP?
Mike Blaszczak
Greenhorn

Joined: Sep 02, 2013
Posts: 22
What do you mean by "in use"? If you connect to that port, you're using it. If you can't connect to the port, then either the machine isn't reachable, isn't running, or is not listening on that port. Maybe you should give more information about your intention so that you might get better help.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Mike Blaszczak wrote:What do you mean by "in use"? If you connect to that port, you're using it. If you can't connect to the port, then either the machine isn't reachable, isn't running, or is not listening on that port. Maybe you should give more information about your intention so that you might get better help.


But in UDP it does not connect to it - it is just sends packages to it and then wait for answer.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Hi James,

I think it is hard to get reliable information regarding used ports with a solution in pure Java and without access to the remote machine. Is the server host under your control? Which OS is it running? Without knowing more about your setup I could think of two possible solutions.

If the server is running on UNIX/Linux you could issue a 'netstat' command via SSH (this is possible from Java) and parse its output for used sockets or open ports. Of course this will only work if you have an SSH account on the remote machine. But at least it will allow you to get a list of ports which are really in use without trying to connect and waiting for a timeout or something like this.

Another solution which should be more portable would be to setup an SNMP service on the remote machine and configure it to provide the information you need. Then you could query the SNMP service from Java (for example with SNMP4J) and simply ask it to tell you which ports are in use on the remote machine. Unfortunately I don't have experience with SNMP on Windows, but I guess this solution should be usable for UNIX/Linux and Windows platforms.

Depending on your requirements and situation there are surely other solutions as well. Maybe you should as tell a little bit more about your problem, as Mike already said.

Marco
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Marco Ehrentreich wrote:Hi James,

I think it is hard to get reliable information regarding used ports with a solution in pure Java and without access to the remote machine. Is the server host under your control? Which OS is it running? Without knowing more about your setup I could think of two possible solutions.

If the server is running on UNIX/Linux you could issue a 'netstat' command via SSH (this is possible from Java) and parse its output for used sockets or open ports. Of course this will only work if you have an SSH account on the remote machine. But at least it will allow you to get a list of ports which are really in use without trying to connect and waiting for a timeout or something like this.

Another solution which should be more portable would be to setup an SNMP service on the remote machine and configure it to provide the information you need. Then you could query the SNMP service from Java (for example with SNMP4J) and simply ask it to tell you which ports are in use on the remote machine. Unfortunately I don't have experience with SNMP on Windows, but I guess this solution should be usable for UNIX/Linux and Windows platforms.

Depending on your requirements and situation there are surely other solutions as well. Maybe you should as tell a little bit more about your problem, as Mike already said.

Marco

Well It is just laboratory task. Only IP is known.

Can you provide netstat example with Java?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check whether port is available on remote pc using tcp and udp
 
Similar Threads
No buffer space available problem
check if a UDP port is busy
How to find out if address is in use
Sending a http-request without waiting for response
how to check for open ports of remote computer in java