java se 网络编程中遇到的问题

smuhm 2014-05-19 05:59:53
import java.net.InetAddress ;
public class InetAddressDemo{
public static void main(String args[]) throws Exception {// 所有异常抛出
InetAddress locAdd = null ;
InetAddress remAdd = null ;
locAdd = InetAddress.getLocalHost() ;// 得到本机
remAdd = InetAddress.getByName("http://www.baidu.com/") ;
System.out.println("本机的IP地址:" + locAdd.getHostAddress()) ;
System.out.println("MLDNJAVA的IP地址:" + remAdd.getHostAddress()) ;
System.out.println("本机是否可达:" + locAdd.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.getAddressesFromNameService(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:7)
...全文
160 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzheqwe 2014-05-20
  • 打赏
  • 举报
回复
tony4geek 2014-05-20
  • 打赏
  • 举报
回复
你连接不上网吧。
yufengdxw 2014-05-20
  • 打赏
  • 举报
回复
在连接外网的情况下,是不会出错的,代码没什么问题;
Kenzson 2014-05-19
  • 打赏
  • 举报
回复
        remAdd = InetAddress.getByName("www.baidu.com");

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧