import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;//inetaddress
public class test112 {public static void main(String[] args){try {InetAddress add0 = InetAddress.getLocalHost();//获取本机ip地址实例System.out.println( "本机服务器名称"+add0.getHostName() );System.out.println( "本地地址"+ add0.getHostAddress() );InetAddress baiduIp=InetAddress.getByName( "www.baidu.com" );System.out.println( "百度服务器名称"+baiduIp.getHostName() );System.out.println( "百度地址"+ baiduIp.getHostAddress() );}catch (UnknownHostException e){e.printStackTrace();}//创建基于给定的端口号的套接字地址实列InetSocketAddress isa=new InetSocketAddress( "localhost",9090 );System.out.println( isa.getHostName() );System.out.println( isa.getHostString() );System.out.println( isa.getAddress().getHostAddress() );}
}
运行结果