<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.net.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试Java的网络功能</title>
</head>
<body>
<%
java.net.InetAddress localhostAddress = java.net.InetAddress.getLocalHost();
// 返回的都是字符串格式的
System.out.println("本机地址:" + localhostAddress + " 主机名:" + localhostAddress.getHostName() + "主机ip:" + localhostAddress.getHostAddress());
out.println("<br>本机地址:" + localhostAddress + " 主机名:" + localhostAddress.getHostName() + "主机ip:" + localhostAddress.getHostAddress());
// 找一个网站的地址看是否可以到达
InetAddress webAddress = InetAddress.getByName("
www.tainfo.net");
// 给定一个互联网的主机名,测试是否可达,以毫秒计算的。
System.out.println("网站" + webAddress.getHostName() + "的IP地址:" + webAddress.getHostAddress() + " 能访问到吗:" + webAddress.isReachable(3000));
out.println("<br>网站" + webAddress.getHostName() + "的IP地址:" + webAddress.getHostAddress() + " 能访问到吗:" + webAddress.isReachable(3000));
webAddress = InetAddress.getByName("localhost");
// 在本机上测试。
System.out.println("使用localhost访问本机" + webAddress.getHostName() + "的IP地址:" + webAddress.getHostAddress() + " 能访问到吗:" + webAddress.isReachable(3000));
out.println("<br>使用localhost访问本机" + webAddress.getHostName() + "的IP地址:" + webAddress.getHostAddress() + " 能访问到吗:" + webAddress.isReachable(3000));
// 使用ip地址测试
webAddress = InetAddress.getByAddress(new byte[]{(byte) 202, (byte) 102, (byte) 152, (byte) 3});
// 在本机上测试。
System.out.println("访问DNS服务器 202.102.152.3" + webAddress.getHostName() + "的IP地址:" + webAddress.getHostAddress() + " 能访问到吗:" + webAddress.isReachable(3000));
out.println("<br>电信DNS服务器 202.102.152.3, DNS主机名" + webAddress.getHostName() + "的IP地址:" + webAddress.getHostAddress() + " 能访问到吗:" + webAddress.isReachable(3000));
%>
</body>
</html>