String getHostAddress()
Returns the IP address string "%d.%d.%d.%d". String SID=localHostAddress.getHostAddress();
Returns the IP address string "%d.%d.%d.%d". String SID=localHostAddress.getHostAddress();
调试欢乐多
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);
byte[] ID=new byte[20];
ID=localHostAddress.getAddress();
String SID=localHostAddress.getHostName();
System.out.println(ID);//这里为什么不能返回IP地址? System.out.println(SID);
System.out.print( "IP is :");
for(int i=0;i<4;i++)
{
int a = ID[i]&0xFF;
System.out.print(a);
if(i<3)
System.out.print(".");
}
System.out.println();
}
}
catch (Exception e)
{
System.out.println("Error: " + e);
}
}
}