Socket编程之-------简单获取IP地址

来源:互联网 发布:南风知我意2 编辑:程序博客网 时间:2024/05/22 00:08

package com.wodwl.example;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIp {
 public static void main(String[] args) {
  InetAddress addr1 = null;
  InetAddress addr2 = null;
  InetAddress addr3 = null;
  String baidu = "www.baidu.com";
  String google = "www.google.com";
  String local = "localhost";
  try {
   addr1 = InetAddress.getByName(baidu);
   addr2 = InetAddress.getByName(google);
   addr3 = InetAddress.getByName(local);
   System.out.println(addr1.getHostName() + "==>"
     + addr1.getHostAddress());
   System.out.println(addr2.getHostName() + "==>"
     + addr2.getHostAddress());
   System.out.println(addr3.getHostName() + "==>"
     + addr2.getHostAddress());
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
 }
}

运行结果:

www.baidu.com==>202.108.22.43
www.google.com==>64.233.189.99
localhost==>64.233.189.99