怎样根据机器IP获得主机名!?

thurdbird 2003-11-16 03:16:34
InetAddress类好象没有这功能!?
...全文
24 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Maxdell 2003-11-16
  • 打赏
  • 举报
回复
import java.net.*;
public class getHostname{
public static void main(String[] args)throws UnknownHostException{

InetAddress[] hostname=InetAddress.getAllByName("www.163.com");
for(int i=0;i<hostname.length;++i)
System.out.println(hostname[i]);
}
}
ipotang 2003-11-16
  • 打赏
  • 举报
回复
如果在局域网内,一个IP对应多个主机,有多个主机名,那如何?
okwuzhijun 2003-11-16
  • 打赏
  • 举报
回复
sorry
请把这行代码删去
InetAddress a = InetAddress.getByName(args[0]);
okwuzhijun 2003-11-16
  • 打赏
  • 举报
回复
在byte[] 数组中,元素的值大于127时就用(byte) 强制转换一下
okwuzhijun 2003-11-16
  • 打赏
  • 举报
回复
import java.net.*;

public class WhoAmI {
public static void main(String[] args) throws Exception {
InetAddress a = InetAddress.getByName(args[0]);
byte[] b = {(byte) 192, (byte) 168, (byte) 128, 01};
InetAddress a = InetAddress.getByAddress(b);
System.out.println(a.getHostName());
System.out.println(a.getHostAddress());
}
}
Maxdell 2003-11-16
  • 打赏
  • 举报
回复
i did it.look at the code below:
---------------------------------
import java.net.*;
public class getHostname{
public static void main(String[] args)throws UnknownHostException{
InetAddress address=InetAddress.getLocalHost();
System.out.println(address); //print host IP
String hostname=address.getHostName(); //get host name
System.out.println(hostname); //print host name
}
}

62,616

社区成员

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

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