Java HTTP Port Find isReachableByPing(String host, int port)

Here you can find the source of isReachableByPing(String host, int port)

Description

is Reachable By Ping

License

Open Source License

Declaration

public static boolean isReachableByPing(String host, int port) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . j  av  a2  s .com*/
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

import java.io.IOException;

import java.net.Socket;

public class Main {
    public static boolean isReachableByPing(String host, int port) {
        Socket ignored = null;
        try {
            ignored = new Socket(host, port);
            ignored.close();
            return true;
        } catch (IOException e) {
            return false;
        }

        //      try{
        //          // nc -zv <hostname/ip> <port/port range>
        //               String cmd = "";
        //               if(System.getProperty("os.name").startsWith("Windows")) {   
        //                       // For Windows
        //                       cmd = "nc -zv " + host + " " + port;
        //               } else {
        //                       // For Linux and OSX
        //                  cmd = "nc -zv " + host + " " + port;
        //               }
        //
        //               Process myProcess = Runtime.getRuntime().exec(cmd);
        //               myProcess.waitFor();
        //
        //               if(myProcess.exitValue() == 0) {
        //
        //                       return true;
        //               } 
        //
        //                return false;
        //               
        //
        //       } catch( Exception e ) {
        //
        //               e.printStackTrace();
        //               return false;
        //       }
    }
}

Related

  1. isPortUsed(final int portNumber, final String host)
  2. isPortUsed(int port)
  3. isPortUsing(String host, int port)
  4. isPortUsing(String host, int port)
  5. isReachable(final String hostName, int port, int timeout)
  6. isServerListening(String host, int port)
  7. isServerListening(String host, int port)
  8. isServerPortFree(int port)
  9. isServing(String host, int port)