在下面的程序中出现的问题:
import java.net.InetAddress;
public class InetAddressDemo {
public static void main(String[] args) throws Exception {
InetAddress local = null;
InetAddress remote = null;
local = InetAddress.getLocalHost();
remote = InetAddress.getByName("http://www.baidu.com/");
System.out.println("本地地址为: " + local.getHostAddress());
System.out.println("远程地址为: " + remote.getHostAddress());
System.out.println("本地可达: " + local.isReachable(5000));
}
}
本来以为没有错误的,但是出现以下错误
Exception in thread "main" java.net.UnknownHostException: http://www.baidu.com/
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at InetAddressDemo.main(InetAddressDemo.java:23)
本来以为是网络连接有问题但是结果不是这方面的原因。必须把remote = InetAddress.getByName("http://www.baidu.com/"); 改为remote = InetAddress.getByName("www.baidu.com");
通过这一个小问题,我明白不能只看课本上如何写,还必须自己动手实践。要提高自己解决问题的能力。
|
|