zoukankan      html  css  js  c++  java
  • 地址及端口的控制

    InetAddress

    Dns:域名解析,将ip地址解析为英文字母

    案例1:获取本机ip和名字

    public static void main(String[] args) {
            try {
                InetAddress addr=InetAddress.getLocalHost();
                System.out.println(addr.getHostAddress());
                System.out.println(addr.getHostName());
                
                addr=InetAddress.getByName("www.baidu.com");
                System.out.println(addr.getHostAddress());
                System.out.println(addr.getHostName());
                
                addr=InetAddress.getByName("61.135.253.15");
                System.out.println(addr.getHostAddress());
                System.out.println(addr.getHostName());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

    运行结果:

    191.167.126.1
    LAPTOP-D4D6919B
    61.135.169.121
    www.baidu.com
    61.135.253.15
    61.135.253.15

    InetSocketAddress

    用于把InetAddress加上端口进行封装

    案例二:对本机网络进行封装

        public static void main(String[] args) {
            InetSocketAddress address=new InetSocketAddress("127.0.0.1",9999);
            System.out.println(address.getHostName());
            System.out.println(address.getPort());
            InetAddress addr=address.getAddress();
            System.out.println(addr.getHostName());
            System.out.println(addr.getHostAddress());
        }

    控制台:

    steamcommunity.com
    9999
    steamcommunity.com
    127.0.0.1
  • 相关阅读:
    Linux-线程同步(day14续)
    Linux之线程(day14)
    Linux-网络编程-UDP网络编程(day13续2)
    ES6 模块加载
    let与var声明区别
    vue 常用指令v-if v-else v-show v-for
    动态路由的意义,以及路由重定向
    前端路由的理解
    socpe 与 包的引入
    VUE 组件注册(全局、局部)
  • 原文地址:https://www.cnblogs.com/littlepage/p/10212093.html
Copyright © 2011-2022 走看看