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); }
}
}
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); }
}
}
我试时候通常返回的是host参数,只有用本地的地址才能返回“name/address”coowoo(coowoo)
gethostbyaddr是那个类的方法
{ 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地址!
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());
}
}
2.参数是ping不到的ip也能输出,没有异常。文档上说“UnknownHostException - if
no IP address for the host could be found.”
如果你在连网情况下,一般都不会有EXCEPTION的,你可以这样:输入一个错误的IP:192。1。1它会抛出异常的!
但是网上另一台主机(win98),我能确定ip没错,也确实有主机名,用这段程序就不行。
显示为ip/ip
你确定它有主机器名吗!?WIN98的我没试过NT,2000,LINUX的好象都没问题的!
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
我是2000+jdk 1.2, 我的目标是一个win98的机器,肯定有主机名,他通过局域网接入internet。
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.
what do you mean by 主机名? machine name?? tell me, what is 主机名 for 207.25.71.5?
java InetAddressTest www.csdn.net;也能放过来得到IP地址的!