import java.net.InetAddress; import java.net.UnknownHostException; public class Address { public static void main(String[] args){ InetAddress ip; try { ip=InetAddress.getLocalHost(); String localname=ip.getHostName();//获取本机名 String localip=ip.getHostAddress();//获取本机地址 System.out.println("本机名:"+localname); System.out.println("本机地址:"+localip); } catch (UnknownHostException e) { System.out.println("主机不存在或网络连接错误"); e.printStackTrace(); } } }
InetAddress类的getHostName()和getHostAddress()方法获得本地主机的本机名和IP地址
最新推荐文章于 2023-05-03 21:51:37 发布