The method of java calling ping and the use of InetAddress class

Java provides the java.net package for the network. The classes such as URL and URLConnection under the package provide the functions of web services that have been accessed programmatically.
Java provides the InetAddress class to represent the IP address.
IntetAddress does not have a constructor, but provides the following two static methods to obtain an IntAddress instance.

  1. getByName(): Obtain the corresponding InetAddrress object according to the host.
  2. getByAddress(): Obtain the corresponding InetAddress() object according to the original IP address
  3. He provides three methods to get the ip address and host name corresponding to the InetAddress instance:
    ①String getCanonicalHostName(): used to get a host name in the local area network
    ②String getHostAddress(): get the string of the ip address
    ③String getHostName(): get The host alias
    InetAddress of the ip address also provides an isReachable(timeout) method to test whether the address (host) can be reached. The firewall or server configuration may block the request.
    Code snippet
  import java.net.InetAddress;

public class ping {
    public static void main(String[] args) {
        try {
            //通过InetAddress类访问主机名,ip地址
            InetAddress indress = InetAddress.getByName("www.baidu.com");//获取对应Ip的inetAddress对象 存储了IP的主机名和ip地址
            String hostName = indress.getHostName();                //获取主机名
            String hostAddress = indress.getHostAddress();          //获取ip地址
            String hostAddress1 = indress.getCanonicalHostName();   //获取ip地址
            System.out.println(hostAddress1);                      //输出ip地址
            System.out.println(indress);                           //输出主机名/ip地址
            System.out.println(hostName);                          //输出主机名
            System.out.println(hostAddress);                      //输出ip地址
            System.out.println("----------------");
            //获取本机地址
            InetAddress localHost = InetAddress.getLocalHost();    //获取本机地址
            String hostName1 = localHost.getHostName();
            String hostAddress2 = localHost.getHostAddress();
            String canonicalHostName = localHost.getCanonicalHostName();
            System.out.println(hostName1);
            System.out.println(hostAddress2);
            System.out.println(canonicalHostName);
            System.out.println("----------------");
            //通过ip获取InetAddress实例获取对应ip地址的信息
            InetAddress byAddress = InetAddress.getByAddress(new byte[]{127,0,0,1});
            System.out.println("ip的主机: "+byAddress.getHostName()+"   ip的ip: "+byAddress.getHostAddress());
            System.out.println("----------------");
           //返回sougou连接结果
            boolean ping = ping("www.sogou.com");
            System.out.println(ping);

        } catch (Exception e) {
            System.out.println("这个ip地址不可用");
            e.printStackTrace();

        }
    }
    public static boolean ping(String ipAddress) throws Exception {
        int  timeOut =  3000 ;  //超时应该在3钞以上
        //isReachable(timeout)的方法,用于测试是否可以到达该地址(主机)
        boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);
        // 当返回值是true时,说明host是可用的,false则不可。
        return status;
    }
}

Guess you like

Origin blog.csdn.net/ssdssa/article/details/110100394