码迷,mamicode.com
首页 > 其他好文 > 详细

获取IP地址

时间:2018-09-04 17:03:30      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:获取   域名   system   bsp   class   cat   之间   特殊   cal   

IP(Internet Protocol 网络之间的互联协议)。IP地址是电脑/服务器的身份证,具有唯一性。

IPv4有4段,由“.”分隔,每个范围0-255,如192.168.0.1

由于IPv4数量有限,因此出现了IPv6(重点掌握IPv4)

IPv6有8段,由“:”分隔,如1080:0:0:0:8:800:200C:417A   

特殊IP地址,本地会送地址(不访问其他网络,访问本地):127.0.0.1

【格式】

InetAddress host=InetAddress.getLocalHost() 获取本地主机

host.getHostName() 主机名

local.getHostAddress() 主机IP地址

InetAddress host=InetAddress.getByName() 获取指定主机

参数可以是IP地址"192.168.0.01"、域名地址“www.baidu.com”、计算机名“Jv”

InetAddress[ ] host=InetAddress.getAllByName() 获取指定主机的所有IP地址

 技术分享图片

public class Demo {
    public static void main(String[] args) {
        try {
            InetAddress local = InetAddress.getLocalHost();//获取本地主机对象
            System.out.println("计算机名:" + local.getHostName());//本地主机名
            System.out.println("本地IP:" + local.getHostAddress());//本地主机IP地址
            InetAddress baidu = InetAddress.getByName("www.baidu.com");//获取百度服务器对象
            System.out.println("百度IP:" + baidu.getHostAddress());
            InetAddress[] baidus = InetAddress.getAllByName("www.baidu.com");
            for (InetAddress ad : baidus) {//大型网站有多个IP,减少宕机损失
                System.out.println("百度IP:" + ad.getHostAddress());//输出所有IP
            }
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }
    }
}

 

获取IP地址

标签:获取   域名   system   bsp   class   cat   之间   特殊   cal   

原文地址:https://www.cnblogs.com/xixixing/p/9584535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!