Java-网络初学01

getLocalHost方法

代码来源:java核心技术卷二第四章网络

package inetAddress;

import java.io.*;
import java.net.*;

/**
 * This program demonstrates the InetAddress class. Supply a host name as command-line
 * argument, or run without command-line arguments to see the address of the local host.
 * @version 1.02 2012-06-05
 * @author Cay Horstmann
 */
public class InetAddressTest
{
   public static void main(String[] args) throws IOException
   {
      if (args.length > 0)
      {
         String host = args[0];
         InetAddress[] addresses = InetAddress.getAllByName(host);
         for (InetAddress a : addresses)
            System.out.println(a);
      }
      else
      {
         InetAddress localHostAddress = InetAddress.getLocalHost();
         System.out.println(localHostAddress);
      }
   }
}

如果不在命令行中设置任何参数,打印本地主机的IP地址,如果在命令行中指定了主机名,打印该主机所有IP地址
在这里插入图片描述

然后

在这里插入图片描述
115.24.160.45原来是校园网
在这里插入图片描述
在天津的河北工大
在这里插入图片描述
杭州的PTA
在这里插入图片描述

北京的百度

在这里插入图片描述
谷歌这个直接德国法兰克福
在这里插入图片描述

posted @ 2021-09-18 21:17  在天边偷看小天使  阅读(6)  评论(0)    收藏  举报  来源