如何获得域名 主机名 的IP地址

wangmeiguang 2007-07-21 01:08:32
如题
...全文
251 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangmeiguang 2007-07-22
  • 打赏
  • 举报
回复
感谢 感谢 xizhiyao 先到先得不好意思
  • 打赏
  • 举报
回复
import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIP {
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getByName("http://localhost:8080");
} catch (UnknownHostException e) {
System.exit(2);
}
System.out.println(address.getHostName() + "="
+ address.getHostAddress());
System.exit(0);
}
}
  • 打赏
  • 举报
回复
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DNSLookup {
public static void main(String args[]) {
try {
InetAddress host;
if (args.length == 0) {
host = InetAddress.getLocalHost();
} else {
host = InetAddress.getByName(args[0]);
}
System.out.println("Host:'" + host.getHostName()
+ "' has address: " + host.getHostAddress());
byte bytes[] = host.getAddress();
int fourBytes[] = new int[bytes.length];
for (int i = 0, n = bytes.length; i < n; i++) {
fourBytes[i] = bytes[i] & 255;
}
System.out.println("\t" + fourBytes[0] + "." + fourBytes[1] + "."
+ fourBytes[2] + "." + fourBytes[3]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
真的很小奚 2007-07-21
  • 打赏
  • 举报
回复
import java.net.InetAddress;
import java.net.UnknownHostException;
  public class NsLookup {
   static public void main(String[] args) {
   try {
  InetAddress address = InetAddress.getByName(args[0]);
  System.out.println(args[0]+“
  : “+address.getHostAddress());
   }
   catch(UnknownHostException uhe) {
  System.err.println(“Unable to find: “+args[0]);
   }
这个是用控制台的 在控制台输入 主机名 或域名 显示IP地址 测试过能用的

62,623

社区成员

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

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