import java.net.*; public class IPAddressTest{ public static void main(String[] args){ try{ //获得本机的InetAddress信息 InetAddress IP = InetAddress.getLocalHost(); showInfo(IP); //从名字获得 InetAddress信息 IP = InetAddress.getByName("www.sina.com.cn"); showInfo(IP); //从IP 地址 获得 InetAddress信息 byte[] bs = new byte[]{(byte)61,(byte)172,(byte)201,(byte)194}; IP = InetAddress.getByAddress(bs); showInfo(IP); } catch(java.net.UnknownHostException e){ e.printStackTrace(); } } //将InetAddress 中的信息显示出来 public static void showInfo(InetAddress IP){ String name = IP.getHostName(); String address = IP.getHostAddress(); System.out.println(name); System.out.println(address); System.out.println("------------------------------"); } }