package cn.hiluna.demo;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 表示互联网中的IP地址
* java.net.InetAddress
* 静态方法
* static InetAddress getLocalHost() localhost本地主机
* 返回本地主机,返回值InetAddress对象
*
* static InetAddress getByName(String hostName) 传递一个主机名,获取IP地址对象
*
* 非静态方法
* String getHostAddress() 获取主机IP地址
* String getHostName() 获取主机名字
*/
public class InetAddreddDemo {
public static void main(String[] args) throws UnknownHostException{
function();
function_1();
}
public static void function_1()throws UnknownHostException{
InetAddress inetAddress = InetAddress.getByName("DESKTOP-BVAU4TG");
System.out.println(inetAddress);
}
/**
* static InetAddress getLocalHost() localhost本地主机
*/
public static void function() throws UnknownHostException {
InetAddress inetAddress = InetAddress.getLocalHost();
//输出结果就是主机名和IP地址
//System.out.println(inetAddress);
String ip = inetAddress.getHostAddress();
String name = inetAddress.getHostName();
System.out.println(ip+name);
// String host = inetAddress.toString();
// String[] strings = host.split("/");
// for (String s : strings){
// System.out.println(s);
// }
}
}
暂无评论