Forums Register Login

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
All of the following truths are shameless lies. But what about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2913 times.
Similar Threads
how to check for open ports of remote computer in java
check if a UDP port is busy
How to find out if address is in use
No buffer space available problem
Sending a http-request without waiting for response
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:17:19.