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

来源:互联网 发布:淘宝上的代练 编辑:程序博客网 时间:2024/06/05 09:02

看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);}

}

原创粉丝点击