Solved

java: tcp port status remote machine

Posted on 2011-03-22
5
738 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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