得到本机IP:
import java.net.*;public class useAddress 
{
public static void main (String[] args) 
{
try 
{
//返回运行程序主机的域名和IP地址
InetAddress address = InetAddress.getLocalHost( );
System.out.println(address);
}
catch (UnknownHostException e) 
{
System.out.println("Could not find this computer's address.");
}
}
}获得给定主机名IP:
import java.net.*;public class useIpAddress 
{
public static void main (String[] args) 
{
try 
{
InetAddress address = InetAddress.getByName("xxx.xxx.xxx");
//输出主机名
System.out.println(address.getHostName( ));
InetAddress address = InetAddress.getByName("www.sohu.com");
//输出指定机器的IP地址
System.out.println(address);
}
catch (UnknownHostException e) 
{
System.err.println(e);
}
}
}

解决方案 »

  1.   

    //: WhoAmI.java
    // Finds out your network address when you're 
    // connected to the Internet.
    import java.net.*;public class WhoAmI {
      public static void main(String[] args) 
          throws Exception {
        if(args.length != 1) {
          System.err.println(
            "Usage: WhoAmI MachineName");
          System.exit(1);
        }
        InetAddress a = 
          InetAddress.getByName(args[0]);
        System.out.println(a);
      }
    }