Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

java: tcp port status remote machine

Posted on 2011-03-22
5
735 Views
Last Modified: 2012-05-11
Trying to ping remote port to see if it’s active or not. Below is an example, it only seems to check the ip address only not port.  
Thank  you

public static void main(String args[]) {

        //Creates an unresolved socket
        InetSocketAddress inetSocketAddress =
                InetSocketAddress.createUnresolved("123.123.2.3", 8181);
        
        // Gets the InetAddress
        System.out.println(inetSocketAddress.getHostName());
    }

Open in new window

0
Comment
Question by:habte
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35189776
Try


InetSocketAddress inetSocketAddress =
                new InetSocketAddress("123.123.2.3", 8181);
boolean up = inetSocketAddress.getAddress().isReachable(5000);  // 5 seconds attempt

Open in new window

0
 

Author Comment

by:habte
ID: 35190591
i tried that, but i get "true" return even if the port is not available.
This only checking the ip address is reachable
E.g, I put port number 9999, and the return value was true. And when I check “telnet localhost 9999” that port is not active.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35190689
OK. If you happen to know protocol in question, just use it on that port - that will tell you. Or simply try to open an InputStream on a Socket at that address
0
 
LVL 92

Expert Comment

by:objects
ID: 35194893
all you need is:

       InetAddress address = InetAddress.getByName("w123.123.2.3");
       boolean reachable = address.isReachable(timeout);

more info:
http://www.rgagnon.com/javadetails/java-0093.html
http://www.kodejava.org/examples/109.html
http://stackoverflow.com/questions/2448666/how-to-do-a-true-java-ping-from-windows
0
 
LVL 92

Expert Comment

by:objects
ID: 35194894
(the w is a typo :)


       InetAddress address = InetAddress.getByName("123.123.2.3");
       boolean reachable = address.isReachable(timeout);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
Apps blocked by Java 9 88
how to install java on RHEL image on EC2 4 40
runtime exception 2 41
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question