java 网络编程三要素之ip地址

看InetAddress的成员方法:

public static InetAddress getByName(String host):根据主机名或者IP地址的字符串表示得到IP地址对象

举例:

public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
    // public static InetAddress getByName(String host)
    // InetAddress address = InetAddress.getByName("qinwendou");
      InetAddress address = InetAddress.getByName("192.168.1.105");


    // 获取两个东西:主机名,IP地址
    // public String getHostName()
    String name = address.getHostName();
    // public String getHostAddress()
    String ip = address.getHostAddress();
    System.out.println(name + "---" + ip);
}

}

猜你喜欢

转载自blog.csdn.net/qinwendou/article/details/78692215