日期:2014-05-20  浏览次数:20934 次

InetAddress.getByName抛出异常的问题
检测网络是否畅通的一段代码,怎么会有异常抛出?下面是部分代码:

writeLog("===========开始检测百度www.baidu.com======\n");
try{
host = InetAddress.getByName("www.baidu.com").toString();
}catch(UnknownHostException e){
e.printStackTrace();
}
try{
status = InetAddress.getByName(host).isReachable(timeOut);  
}catch(IOException e){
e.printStackTrace();
}  
if(status){
writeLog("成功\n");
}else{
writeLog("网站访问不到,\n");
}

结果确实输出了成功,说明ping www.baidu.com也成功了,可是会有这样的提示:
java.net.UnknownHostException: www.baidu.com/61.135.169.125
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 ping.ActionHandle.checkServer(PingSwtich.java:247)
at ping.ActionHandle$2.actionPerformed(PingSwtich.java:39)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
异常

------解决方案--------------------
楼主不应该啊,应该是粗心吧。。
host = InetAddress.getByName("www.baidu.com").toString(); 
这句话如果打印的话结果是
www.baidu.com/61.135.169.125
然后你用这个去
status = InetAddress.getByName(host).isReachable(timeOut); 
当然会报异常了。。
改成
host = InetAddress.getByName("www.baidu.com").getHostName();就可以了。
------解决方案--------------------
status = InetAddress.getByName("www.baidu.com").isReachable(timeOut);  

这样写应该就可以了
不过返回的是false,用ping可以
public boolean isReachable(int timeout)
测试是否可以达到该地址。实现尽最大努力试图到达主机,但防火墙和服务器配置可能阻塞请求,使其在某些特定的端口可以访问时处于不可到达状态。