tryimport java.io.*;
import java.net.*;public class TestIP { 
 public static void main(String[] args){ 
  String azIP02, azWWW; 
  azIP02 = "207.25.71.25"; 
  try{ 
   InetAddress IntAdrss00 = InetAddress.getByName(azIP02); 
   azWWW = IntAdrss00.getHostName(); 
   System.out.print( " " + azWWW ); 
  } catch( IOException ioe ){ System.out.println(" Error " + ioe); } 
 }
}

解决方案 »

  1.   

    karma(无为)
    我试时候通常返回的是host参数,只有用本地的地址才能返回“name/address”coowoo(coowoo)
    gethostbyaddr是那个类的方法 
      

  2.   

    import java.net.*;public class InetAddressTest
    {  public static void main(String[] args)
       {  try
          {
             if (args.length > 0)
             {  String host = args[0];
                InetAddress[] addresses
                   = InetAddress.getAllByName(host);
                for (int i = 0; i < addresses.length; i++)
                   System.out.println(addresses[i]);
             }
             else
             {  InetAddress localHostAddress= InetAddress.getLocalHost();
                System.out.println(localHostAddress);
             }
          }
          catch (Exception e)
          {  System.out.println("Error: " + e);
          }
       }
    }
    获得输入参数的IP地址!
      

  3.   

    输入你自己的IP试一下,name为hostname,address为ip地址
        public static void main(String args [])
        {
        try
        {
            if(args.length != 1)
                return;
            else
            {
                java.net.InetAddress ia = java.net.InetAddress.getByName(args[0]);
                String name = ia.getHostName();
                String address = ia.getHostAddress();
                System.out.println(address + "-------------" + name);
            }
        }
        catch(java.net.UnknownHostException uhe)
        {
            System.out.println(uhe.getMessage());
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        }
      

  4.   

    pengji(彭乃超)还是有问题,以ip地址为参数时1.只有localhost可以输出“name/ip”的形式,其它均为“ip/ip”,同karma(无为)的
    2.参数是ping不到的ip也能输出,没有异常。文档上说“UnknownHostException - if
      no IP address for the host could be found.”
      

  5.   

    java_fox()问题同pengji(彭乃超)
      

  6.   

    to dope(吊儿郎当):如果你的机器确实有名字的话显示的应该是:主机器名/IP
    如果你在连网情况下,一般都不会有EXCEPTION的,你可以这样:输入一个错误的IP:192。1。1它会抛出异常的!
      

  7.   

    to pengji(彭乃超)用自己的ip测试是没问题的;
    但是网上另一台主机(win98),我能确定ip没错,也确实有主机名,用这段程序就不行。
    显示为ip/ip
      

  8.   

    to:dope(吊儿郎当)
    你确定它有主机器名吗!?WIN98的我没试过NT,2000,LINUX的好象都没问题的!
      

  9.   

    I bet you do not have DNS, on my machine (W2k professional), after I change
    azIP02 = "207.25.71.25"; 
    to
    azIP02 = args[0];I gotE:\Labs\java>java TestIP 207.25.71.5
     www11.cnn.com
    E:\Labs\java>java TestIP 192.168.112.174
     MyMachineName
      

  10.   

    to pengji(彭乃超)
    我是2000+jdk 1.2, 我的目标是一个win98的机器,肯定有主机名,他通过局域网接入internet。
      

  11.   

    to karma(无为)你说的DNS是在拨号连接的协议属性里设的DNS吗,如果是这个我就有。还有“after I change azIP02 = "207.25.71.25"; to azIP02 = args[0];”有区别吗?最后 www11.cnn.com是地址不是主机名呀
      

  12.   

    下面是我在其它论坛上问的,第一个是找不到方法getByAddress,后两个问题都一样Author: Zdope  Feb 5, 2002 4:15 AM      
     
    how to get hostname of a remote computer by ip 
       Re: how to get hostname of a remote computer by ip 
    Author: ThoralfR  Feb 5, 2002 4:38 AM   Reply 1 of 4   
     
     
    With the wonderful InetAddress class, use something like this...:byte[] addr = new byte[4];
    // maybe from java.net.Socket, but this should work
    addr[0]=192;
    addr[1]=168;
    addr[2]=1;
    addr[3]=1; 
    String hostname = InetAddress.getByAddress(addr).getHostName();  
       Re: how to get hostname of a remote computer by ip 
    Author: deepadatar  Feb 5, 2002 4:51 AM   Reply 2 of 4   
     
     
    In InetAddress class we have a method 
    public String getHostName() which returns the name of the host. If applet security prevents the name, the same ipaddress entered is returned back.This program is the implementation of this method:import java.net.*;
    public class ipdemo
    {
    public static void main(String arg[])
    {
    try
    {
    InetAddress add=InetAddress.getByName("207.228.236.39");
    System.out.println(add.getHostName());
    }
    catch(UnknownHostException e)
    {
    System.out.println("Exception raised");
    }
    }
    }This will help you.Regards.
    Deepa Datar
     
     Re: how to get hostname of a remote computer by ip 
    Author: Zdope  Feb 5, 2002 5:26 AM   Reply 3 of 4   
     
     
    hi, it doesn't work.String hostname = InetAddress.getByAddress(addr). 
    ^
    getHostName();and i can't find getByAddress() in docs of jdk1.3, is this 
    method need a higher version?
     
       Re: how to get hostname of a remote computer by ip 
    Author: abnormal  Feb 5, 2002 5:44 AM   Reply 4 of 4   
     
     
    I think it is in jdk1.4. Try with InetAddress.getByName("207.228.236.39").getHostName() as already mentioned be deepadatar.  
     
      

  13.   

    I change the line so that I can enter an IP address on the command line and get back the host name
    what do you mean by 主机名? machine name?? tell me, what is 主机名 for 207.25.71.5?
      

  14.   

    我这段程序只要你反过来输入:
    java InetAddressTest www.csdn.net;也能放过来得到IP地址的!