利用java InetAddress类获取本机的P地址或根据域名自动到域名服务器DNS上查找IP地址。
源代码:
package FirstPackages;
import java.net.*;
public class GetLocalHost
{
public static void main(String args[])
{
InetAddress myIP=null;
try{
myIP=InetAddress.getLocalHost();
System.out.println(myIP);
myIP=null;
myIP=InetAddress.getByName("www.zhihao.com.cn/");
System.out.println(myIP);
}
catch(UnknownHostException e)
{
System.out.println("DNS错误");
}
}
}
程序运行结果截图:
本文通过一个简单的Java示例展示了如何使用InetAddress类来获取本机的IP地址,以及如何通过域名查询对应的IP地址。
7552

被折叠的 条评论
为什么被折叠?



