IP地址与端口

前言:IP地址与端口可以唯一的确定一个主机应用的位置,其中IP地址可以确定一台主机的地址,端口可以确定主机上进程的地址

IP地址与端口

IP地址

  • 确定网络上主机位置

获取网站域名对应IP地址

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

public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查询百度IP地址
            InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress);
            //查询本机地址
            InetAddress inetAddress1 = InetAddress.getLocalHost();
            System.out.println(inetAddress1);
        } catch (UnknownHostException e){
            e.printStackTrace();
        }
    }
}

执行结果:
![](https://img2020.cnblogs.com/blog/2046280/202005/2046280-20200525210953399-1201318953.png)

猜你喜欢

转载自www.cnblogs.com/Kuris101/p/12960866.html