• 获取地址


     
    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();
            }
        }
     
    }
     
     
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    [JSOI2007][BZOJ1031] 字符加密Cipher|后缀数组
    leetcode Flatten Binary Tree to Linked List
    leetcode Pascal's Triangle
    leetcode Triangle
    leetcode Valid Palindrome
    leetcode Word Ladder
    leetcode Longest Consecutive Sequence
    leetcode Sum Root to Leaf Numbers
    leetcode Clone Graph
    leetcode Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710305.html
Copyright © 2020-2023  润新知