zoukankan      html  css  js  c++  java
  • 获取地址

     
    public class GetIpAddress
    {
        //获取本机IP地址
        public static String getLocalIp()throws UnknownHostException
        {
            InetAddress addr=InetAddress.getLocalHost();
            return addr.getHostAddress();
        }
     
        //获取本机机器名
        public static String getLocalName()throws UnknownHostException
        {
            InetAddress addr=InetAddress.getLocalHost();
            return addr.getHostName();
        }
     
     
        //根据域名获取主机Ip地址
        public static String[] getAllIpByName(String hostName) throws UnknownHostException
        {
            InetAddress []addr=InetAddress.getAllByName(hostName);
            String []ips=new String[addr.length];
            for(int i=0;i<ips.length;i++)
            {
                ips[i]=addr[i].getHostAddress();
            }
            return ips;
        }
        public static String getIpByName(String hostName) throws UnknownHostException
        {
            InetAddress addr=InetAddress.getByName(hostName);
     
            return addr.getHostAddress();
        }
     
        public static void main(String[] args)
        {
            // TODO 自动生成的方法存根
            try
            {
                System.out.println("本机ip: "+getLocalIp());
                System.out.println("本机名字: "+getLocalName());
            //某网站的ip
                String zhihu="www.zhihu.com";
                //System.out.println("知乎的IP:"+getIpByName(zhihu));
     
     
                String []zhiHuIps=getAllIpByName(zhihu);
                System.out.println("知乎的IP:"+zhiHuIps.length);
                for(int i=0;i<zhiHuIps.length;i++)
                System.out.println(zhiHuIps[i]);
     
     
            } catch (Exception e)
            {
                // TODO: handle exception
                System.err.println("找不到网址");
                //e.printStackTrace();
            }
        }
     
    }
     
     
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    Generate profile vspx
    (转)Connect string 中的 Intergrated Security
    删除数据库
    SQL Server问题之The remote procedure call failed. [0x800706be]
    SQL Server 2008 R2如何开启数据库的远程连接
    ubuntu 12.04 sourcelist 更新源
    Ubuntu12.04 命令gedit出错:Could not connect to session bus (
    解决 Ubuntu 12.04 无法调节屏幕亮度的问题
    Eclipse和PyDev搭建完美Python开发环境(Windows篇)
    raw_input() 与 input() __ Python
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710305.html
Copyright © 2011-2022 走看看