java中怎么将域名解析为ip

yzbsd 2007-11-28 10:36:01
程序里面想实现这样的功能,输入域名之后,解析出对应ip,应该如何做?
...全文
2220 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
LIU84010202 2011-08-08
  • 打赏
  • 举报
回复
网络编程中的InetAddress类中的InetAddress.getByName()和InetAddress.getAllByName()都能将域名解析为IP地址:
import java.net.*;
public class Kkkk
{
public static void main(String args[])throws Exception
{
InetAddress address=InetAddress.getByName("Claire");//Claire是我的计算机名
System.out.println(address);
System.out.println("-----");
InetAddress address1=InetAddress.getLocalHost();
System.out.println(address1);
InetAddress[] addresses=InetAddress.getAllByName("http://www.baidu.com");
for(InetAddress addr:addresses)
{
System.out.println(addr);
}
}
}

运行结果为:
Claire/59.73.131.125
-----
Claire/59.73.131.125
http://www.baidu.com/221.238.203.46
lilei9963 2008-09-23
  • 打赏
  • 举报
回复
%<>/&&orand
lilei9963 2008-09-23
  • 打赏
  • 举报
回复
<html>
fdsafsdafdasfads
</html>
lish1210 2008-09-16
  • 打赏
  • 举报
回复
谢谢分享,可实现,测试过!
beiouwolf 2007-11-28
  • 打赏
  • 举报
回复
这个你要查一下DNS的协议文档
通过DNS服务器来查找
cocosunshine 2007-11-28
  • 打赏
  • 举报
回复
ping www.sina.com.cn

Pinging jupiter.sina.com.cn [61.172.201.194] with 32 bytes of data:

Reply from 61.172.201.194: bytes=32 time=3ms TTL=248
Reply from 61.172.201.194: bytes=32 time=3ms TTL=248
Reply from 61.172.201.194: bytes=32 time=3ms TTL=248
Reply from 61.172.201.194: bytes=32 time=3ms TTL=248

Ping statistics for 61.172.201.194:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 3ms, Maximum = 3ms, Average = 3ms
ztc16627 2007-11-28
  • 打赏
  • 举报
回复
解析域名是DNS服务器干的事,如果你无法连到DNS服务器上去的话,就要自己维护域名/IP数据了。
yzbsd 2007-11-28
  • 打赏
  • 举报
回复
我想在j2me中使用,ping的方法肯定不行
cocosunshine 2007-11-28
  • 打赏
  • 举报
回复
java的api有没有还真没有留意过,你可以查看一下java的api document,如果没有你可以用java调用windows ping命令来给与实现。
yifuzhiming 2007-11-28
  • 打赏
  • 举报
回复
好象java编程思想上有的
key_hua 2007-11-28
  • 打赏
  • 举报
回复
6L又输入的话就可以实现了
maskwy 2007-11-28
  • 打赏
  • 举报
回复
真可以实现这个功能吗
cursor_wang 2007-11-28
  • 打赏
  • 举报
回复
有的,我给你们吧.

import java.net.*;

public class NetTool {
InetAddress myIPaddress = null;

InetAddress myServer = null;

public static void main(String args[]) {
NetTool mytool;
mytool = new NetTool();
System.out.println("Your host IP is: " + mytool.getMyIP());
System.out.println("The Server IP is :" + mytool.getServerIP());

}

// 取得LOCALHOST的IP地址
public InetAddress getMyIP() {
try {
myIPaddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
return (myIPaddress);
}

// 取得 www.abc.com 的IP地址
public InetAddress getServerIP() {
try {
myServer = InetAddress.getByName("www.abc.com");
} catch (UnknownHostException e) {
}
return (myServer);
}

}

62,623

社区成员

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

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