• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic