集册 Java实例教程 主机IP查找应用程序

主机IP查找应用程序

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

362
主机IP查找应用

import java.net.InetAddress;/**来自 NowJava.com - 时  代  Java**/

import java.net.UnknownHostException;

import java.util.Scanner;


public class HostLookup {

  static Scanner sc = new Scanner(System.in);


  public static void main(String[] args) {

    System.out.println("Welcome to the IP lookup application.");

    String host;

    do {

      System.out.print("\nEnter a host name: ");

      host = sc.nextLine();

      try {

        InetAddress[] addresses = InetAddress.getAllByName(host);
        /* 
        *来 自
         时代Java - nowjava.com
        */

        for (InetAddress ip : addresses)

          System.out.println(ip.toString());

      } catch (UnknownHostException e) {

        System.out.println("Unknown host.");

      }

    } while (doAgain());

  }


  private static boolean doAgain() {

    System.out.println();

    String s;

    while (true) {

      System.out.print("Look up another? " + "(Y or N) ");

      s = sc.nextLine();

      
展开阅读全文