Solved

java: tcp port status remote machine

Posted on 2011-03-22
5
725 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
(the w is a typo :)


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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now