Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
James Whillis
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic