InetAddress常用操作

来源:互联网 发布:视频网站源码 php 编辑:程序博客网 时间:2024/06/11 22:42

InetAddress类的提供了一些对于IP地址的很多操作。
下面简单演示了几种用法,用到查API就好了。

public static void main(String[] args) throws Exception    {        //返回本地主机的IP地址对象        //InetAddress address = InetAddress.getLocalHost();        //获取任意一台主机的IP地址对象        InetAddress address = InetAddress.getByName("www.baidu.com");        //获取ip地址        String ip = address.getHostAddress();        //获取主机名        String name = address.getHostName();        System.out.println(ip+":"+name);    }
0 0