zoukankan      html  css  js  c++  java
  • java根据主机名获取ip

    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    /*
     *  表示互联网中的IP地址
     *    java.net.InetAddress
     *  静态方法
     *    static InetAddress  getLocalHost()   LocalHost本地主机
     *    返回本地主机,返回值InetAddress对象
     *    
     *    static InetAddress getByName(String hostName)传递主机名,获取IP地址对象
     *    
     *  非静态方法
     *     String getHoustAddress()获取主机IP地址
     *     String getHoustName()获取主机名
     *    
     */
    public class InetAddressDemo {
    	public static void main(String[] args) throws UnknownHostException{
    		for (int i = 0; i < 100; i++) {
    			function();
    		}
    		
    	}
    	
    	public static void function_1() throws UnknownHostException{
    		InetAddress inet = InetAddress.getByName("www.baidu.com");
    		System.out.println(inet.toString());
    	}
    	
    	public static void function() throws UnknownHostException{
    		InetAddress inet = InetAddress.getLocalHost();
    		
    		//输出结果就是主机名,ip地址
    		System.out.println(inet.toString());
    		String ip = inet.getHostAddress();
    		String name = inet.getHostName();
    		System.out.println(ip + "    " + name);
    		
    		System.out.println("-------------");
    		String host = inet.toString();
    		String[] str = host.split("/");
    		for(String s: str) {
    			System.out.println(s);
    		}
    	}
    
    }
    
  • 相关阅读:
    OCP-1Z0-053-V12.02-40题
    OCP-1Z0-053-V12.02-255题
    OCP-1Z0-053-V12.02-407题
    OCP-1Z0-053-V12.02-298题
    OCP-1Z0-053-V12.02-38题
    OCP-1Z0-053-V12.02-274题
    OCP-1Z0-053-V12.02-159题
    OCP-1Z0-053-V12.02-406题
    OCP-1Z0-053-V12.02-369题
    OCP-1Z0-053-V12.02-610题
  • 原文地址:https://www.cnblogs.com/zhaoyongle/p/9242463.html
Copyright © 2011-2022 走看看