0
点赞
收藏
分享

微信扫一扫

【Java InetAddress和InetSocketAddress】

崭新的韭菜 2022-04-13 阅读 42
java
package com.yuzhenc.network;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @author: yuzhenc
 * @date: 2022-04-10 18:10:19
 * @desc: com.yuzhenc.network
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) throws UnknownHostException {
        //封装IP:
        //InetAddress ia = new InetAddress();不能直接创建对象,因为InetAddress()被default修饰了。
        InetAddress ia = InetAddress.getByName("192.168.199.217");
        System.out.println(ia);///192.168.199.217
        //localhost指代的是本机的ip地址
        InetAddress ia2 = InetAddress.getByName("localhost");
        System.out.println(ia2);//localhost/127.0.0.1
        //127.0.0.1指代的是本机的ip地址
        InetAddress ia3 = InetAddress.getByName("127.0.0.1");
        System.out.println(ia3);///127.0.0.1
        //封装计算机名
        InetAddress ia4 = InetAddress.getByName("DESKTOP-UGGVQPG");
        System.out.println(ia4);//DESKTOP-UGGVQPG/2.2.3.67
        //封装域名
        InetAddress ia5 = InetAddress.getByName("www.baidu.com");
        System.out.println(ia5);//www.baidu.com/14.215.177.39
        //获取域名
        System.out.println(ia5.getHostName());//www.baidu.com
        //获取ip地址
        System.out.println(ia5.getHostAddress());//14.215.177.39
        //InetSocketAddress封装ip和端口
        InetSocketAddress isa = new InetSocketAddress("192.168.199.217",8080);
        System.out.println(isa);///192.168.199.217:8080
        System.out.println(isa.getHostName());//192.168.199.217
        System.out.println(isa.getPort());//8080
    }
}
举报

相关推荐

0 条评论