如何通过java判断给定的ip地址是否可连接?

解决方案 »

  1.   

    尝试建立根据特定ip地址和端口号去建立socket连接,看出异常没,就知道ip地址是否可连接了
      

  2.   

    代码差不多这样子public class ConnectFriendly {    public static void main(String[] argv) {        String server_name = argv.length == 1 ? argv[0] : "localhost";        int tcp_port = 80;        try {            Socket sock = new Socket(server_name, tcp_port);            /* Finally, we can read and write on the socket. */            System.out.println(" *** Connected to " + server_name  + " ***");            /* ... */            sock.close( );        } catch (UnknownHostException e) {            System.err.println(server_name + " Unknown host");            return;        } catch (NoRouteToHostException e) {            System.err.println(server_name + " Unreachable" );            return;        } catch (ConnectException e) {            System.err.println(server_name + " connect refused");            return;        } catch (java.io.IOException e) {            System.err.println(server_name + ' ' + e.getMessage( ));            return;        }    }}
      

  3.   

    谢谢,我测试了,为什么连其它机器或本机都显示  connect   refused 呢?
      

  4.   

    2楼的方法不行,ping和socket都不是一层的东西,机器没开80端口你怎么连得上?
    用下面的方法吧        try ...{
                InetAddress address = InetAddress.getByName("127.0.0.1");
                System.out.println(address.isReachable(5000));
            } catch (UnknownHostException e) ...{
                e.printStackTrace();
            } catch (IOException e) ...{
                e.printStackTrace();
            }
      

  5.   

    楼上的方法我试了,但是有防火墙的机器就连不通(能ping通),不知道有什么解决方法。
    可不可以通过特定端口号进行连接测试?
      

  6.   

    Runtime r = Runtime.getRuntime();
    String ip = "172.16.41.108";
    int timeout = 1000;
    String pingCommand = "ping " + ip + " -w " + timeout;
    try {
      Process p = r.exec(pingCommand);
                if (p == null)
                {
                    System.out.println("Failed.");
                }
    BufferedReader in = new BufferedReader(new InputStreamReader(p.
                    getInputStream()));
                String line = null;
                while ( (line = in.readLine()) != null)
                {
                    System.out.println(line);
                    if (line.startsWith("Reply from"))
                    {
    System.out.println("Conected.");
                    }
                }
                in.close();
    }
            catch (Exception ex)
            {
                System.out.println("Failed.");
            }
      

  7.   

    晕,没排版。
        Runtime r = Runtime.getRuntime();
            String ip = "172.16.41.108";
            int timeout = 1000;
            String pingCommand = "ping " + ip + " -w " + timeout;
            try {
                Process p = r.exec(pingCommand);
                if (p == null) {
                    System.out.println("Failed.");
                }
                BufferedReader in = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                    if (line.startsWith("Reply from")) {
                        System.out.println("Conected.");
                    }
                }
                in.close();
            } catch (Exception ex) {
                System.out.println("Failed.");
            }
      

  8.   

    用在linux上import java.lang.Runtime;
    import java.io.*;public class myTest {
    private BufferedReader out;

        public myTest() {
        }
        
        public String myTest(String IP){
         String proOut = "";
         String pLog = "";
         try{
         Process pro = Runtime.getRuntime().exec("ping "+IP+" -w 1");
         out = new BufferedReader(new InputStreamReader(pro.getInputStream()));
         pro.waitFor(); 
         String record = out.readLine();
         while(record != null){
         proOut = proOut + record + "\n";
         record = out.readLine();
         }
         out.close();
         pro.destroy();
         //ping p = new ping();
         if (this.result(proOut).equals("running")){
         pLog = "Server "+IP+" is running.";
         }else{
         pLog = "Server "+IP+" doesn't run.";
         }
         //pLog = p.result(proOut);
         }catch(Exception e){
         pLog = "Script executing error!";
         }
         return pLog;
        
        }
        
        public String result(String s){
    if (s.indexOf("bytes from") != -1){
    return "running";
    }else{
    return "not run";
    }
        }
        
    }
      

  9.   

    不好意思,加入格式。
    import java.lang.Runtime;
    import java.io.*;public class myTest {
    private BufferedReader out;

        public myTest() {
        }
        
        public String myTest(String IP){
         String proOut = "";
         String pLog = "";
         try{
         Process pro = Runtime.getRuntime().exec("ping "+IP+" -w 1");
         out = new BufferedReader(new InputStreamReader(pro.getInputStream()));
         pro.waitFor(); 
         String record = out.readLine();
         while(record != null){
         proOut = proOut + record + "\n";
         record = out.readLine();
         }
         out.close();
         pro.destroy();
         //ping p = new ping();
         if (this.result(proOut).equals("running")){
         pLog = "Server "+IP+" is running.";
         }else{
         pLog = "Server "+IP+" doesn't run.";
         }
         //pLog = p.result(proOut);
         }catch(Exception e){
         pLog = "Script executing error!";
         }
         return pLog;
        
        }
        
        public String result(String s){
    if (s.indexOf("bytes from") != -1){
    return "running";
    }else{
    return "not run";
    }
        }
        
    }