集册 Java实例教程 演示InetAddress类的使用

演示InetAddress类的使用

欢马劈雪     最近更新时间:2020-01-02 10:19:05

245
演示InetAddress类的使用

import java.io.IOException;

import java.net.InetAddress;
/**from n o w    j a v a  . c o m**/

public class Main {

  public static void main(String[] args) {

    printAddressDetails("nowjava.com");


    // Print the loopback address details

    printAddressDetails(null);


    // Print the loopback address details using IPv6 format

    printAddressDetails("::1");

  }


  public static void printAddressDetails(String host) {

    System.out.println("Host '" + host + "' details starts...");


    try {/*来自 n o w  j a v a  . c o m*/

      InetAddress addr = InetAddress.getByName(host);

      System.out.println("Host IP Address: " + addr.getHostAddress());

      System.out.println("Canonical Host Name: " + addr.getCanonicalHostName());


      int timeOutinMillis = 10000;

      System.out.println("isReachable(): " + addr.isReachable(timeOutinMillis));

      System.out.println("isLoopbackAddress(): " + addr.isLoopbackAddress());

    } catch (
展开阅读全文