【1】InetAddress --》封装了Ip
package com.msb.test01; import java.net.InetAddress; import java.net.UnknownHostException; /** * @author : liu * 日期:09:55:44 * 描述:IntelliJ IDEA * 版本:1.0 */ public class Test01 { //这是一个main方法:是程序的入口 public static void main(String[] args) throws UnknownHostException { //封装ip //InetAddress ia=new InetAddress();//不能直接创建对象,因为InetAddress()被default修饰了。 InetAddress ia=InetAddress.getByName("192.168.3.2"); System.out.println(ia); InetAddress ia2=InetAddress.getByName("Localhost");//Localhost指代的是本机的ip地址 System.out.println(ia2); InetAddress ia3=InetAddress.getByName("127.0.0.1");//127.0.0.1指代的是本机的ip地址 System.out.println(ia3); InetAddress ia4=InetAddress.getByName("liudapeng");//封装计算机名 System.out.println(ia4); InetAddress ia5=InetAddress.getByName("www.mashibing.com");//封装域名 System.out.println(ia5); System.out.println(ia5.getHostName());//获取域名 System.out.println(ia5.getHostAddress());//获取地址 } }
【2】InetSocketAddress--》封装了ip,端口号
package com.msb.test01; import java.net.InetAddress; import java.net.InetSocketAddress; /** * @author : liu * 日期:10:23:39 * 描述:IntelliJ IDEA * 版本:1.0 */ public class Test02 { //这是一个main方法:是程序的入口 public static void main(String[] args) { InetSocketAddress isa =new InetSocketAddress("192.168.3.2",8080); System.out.println(isa); System.out.println(isa.getHostName()); System.out.println(isa.getPort()); InetAddress ia = isa.getAddress(); System.out.println(ia.getHostName()); System.out.println(ia.getHostAddress()); } }
标签:System,ia,println,InetAddress,InetSocketAddress,out From: https://www.cnblogs.com/jeldp/p/17003742.html