?
Solved

java: tcp port status remote machine

Posted on 2011-03-22
5
Medium Priority
?
741 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 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