Java中的IP类:InetAddress
定位唯一一台网络上的计算机
127.0.0.1 :本机(localhost)
IP地址的分类
ipv4/ipv6
公网(互联网)- 私网(局域网)
域名:记忆IP问题(IP很值钱!!!)
在Java中使用查找IP
public class IpAddressTest { public static void main(String[] args) { try { //查找本机地址 InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1"); // /127.0.0.1 System.out.println(inetAddress1); InetAddress inetAddress2 = InetAddress.getByName("localhost"); // localhost/127.0.0.1 System.out.println(inetAddress2); InetAddress inetAddress3 = InetAddress.getLocalHost(); // DESKTOP-LIM1T42/172.18.1.164 System.out.println(inetAddress3); //查询网站地址 InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com"); // www.baidu.com/180.101.49.11 System.out.println(inetAddress4); //常用方法 System.out.println(inetAddress4.getAddress()); System.out.println(inetAddress4.getHostAddress()); System.out.println(inetAddress4.getCanonicalHostName()); System.out.println(inetAddress4.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } } }
端口表示一个计算机上程序的进程
在Java中使用端口
public class InetSocketAddressTest { public static void main(String[] args) { InetSocketAddress isa = new InetSocketAddress("127.0.0.1",8080); System.out.println(isa); System.out.println(isa.getAddress()); System.out.println(isa.getHostName()); System.out.println(isa.getHostString()); System.out.println(isa.getPort()); } }