InetAddress,InetSocketAddress
【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()); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本