[java语言]——InetAddress类的getByName()方法

InetAddress---表示互联网协议(IP)地址

   ---InetAddress.getByName("www.163.com")----在给定主机名的情况下确定主机的IP地址

                                                                        ----如果参数为null,获得的是本机的IP地址

import java.net.*;

public class MyHost {
         public static void main(String args[]) {
         InetAddress ip = null;

                      try {
                              ip = InetAddress.getByName("www.baidu.com");// 修改为指定的主机名称
                              System.out.println(ip.toString());
                              System.out.println("主机名:" + ip.getHostName());
                               System.out.println("主机IP地址:" + ip.getHostAddress());
                                System.out.println("本机IP地址:"+ InetAddress.getLocalHost().getHostAddress());
                            } catch (UnknownHostException e) {
                                         e.printStackTrace();
                                                                                   }

                                                                    }
                                  }

原文地址:https://www.cnblogs.com/JasonLGJnote/p/7822992.html