首页 > 其他分享 >InetAddress,InetSocketAddress

InetAddress,InetSocketAddress

时间:2022-12-25 10:45:24浏览次数:45  
标签:System ia println InetAddress InetSocketAddress out

【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

相关文章

  • InetAddress类(IP地址)
    packageday1;importjava.net.InetAddress;importjava.net.UnknownHostException;publicclassInetAddressDemo{//InetAddress类,对应IP地址//IP地址和端口号组合......
  • Muduo库之Endian、SocketsOps、InetAddress和Socket
    EndianEndian.h是一个公共头文件,里面包含了一些网络字节序和主机字节序相互转换的问题。其中所使用的方法如下://XX位主机转网络uint64_thtobe64(uint64_tdata);u......
  • 网络编程概述、网络编程三要素、InetAddress类及端口和协议介绍
    目录​​一、网络编程概述​​​​二、网络编程三要素​​​​IP地址:​​​​端口号:​​​​协议:​​​​三、InetAddress​​​​四、端口和协议​​一、网络编程概述计算......
  • InetAddress.getLocalHost() 执行很慢?
    背景介绍某次在SpringBoot2.2.0项目的一个配置类中引入了这么一行代码:InetAddress.getLocalHost().getHostAddress()导致项目启动明显变慢。同时报出了相关的警告信......
  • InetAddress inetAddress 常用方法
    javaSE进阶之InetAddress类的域名解析与代码实例2021-08-1909:491.通讯要素1:IP和端口号IP地址:InetAddress唯一的标识Internet上的计算机本地回环地址(hostAddress......