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());

    }
}
复制代码

 

posted @   爱的加勒比  阅读(69)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示