7天掌握NIO和SOCKET,第五天,获取硬件物理地址、InetAddress类的获取和使用

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class test_02 {
    public static void main(String[] args) throws Exception {
        //获取硬件物理地址(MAC地址,硬件地址,物理地址含义是一样的,有IEEE统一给厂家分配)
        //物理地址一共48为,因为每4为是十六进制的以为,分为12位十六进制,每两位十六进制为一组
        //一共6组,下面方法里有详细介绍:
        //method_01();

    //本机ipv4地址:变化可以用256去减
    //(byte) -246, (byte) -54, (byte) 133,(byte) 99
    private static void method_01() throws SocketException{
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
                System.out.println("网络设备名称:"+networkInterface.getName());
                System.out.println("网络设备显示名称:"+networkInterface.getDisplayName());
                System.out.print("网卡的物理地址:");
                byte[] bytes = networkInterface.getHardwareAddress();
                if(bytes!=null && bytes.length!=0)
                for (int i = 0 ; i < bytes.length ; i++){
                    System.out.print(bytes[i]);
                }

        }
        /*某一个输出为:
        * -128,-6,91,89,66,-27,而cmd打开,输入ipconfig -all有一个输出为:
        *80-FA-5B-59-42-E5
        * (负数运算,256-128 = 128 = 80(十六进制))
        * 刚刚好是对应的
        * */
    }
        //获得IP地址的基本信息:
        //method_02();

    private static void method_02() throws SocketException{
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            System.out.println("网络设备名称:"+networkInterface.getName());
            System.out.println("网络设备的显示名称:"+networkInterface.getDisplayName());
            System.out.println("网络接口的InetAddress信息:");
            Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses();
            while (enumeration.hasMoreElements()){
                InetAddress inetAddress = enumeration.nextElement();
                System.out.println("\t此IP地址的完全限定名:"+inetAddress.getCanonicalHostName());
                System.out.println("\t此IP地址的主机名:"+inetAddress.getHostName());
                System.out.println("\t此IP地址的字符串:"+inetAddress.getHostAddress());
                System.out.print("\t此对象的原始IP地址:");
                byte[] bytes = inetAddress.getAddress();
                for (int i = 0 ; i < bytes.length ; i++){
                    System.out.print(bytes[i]);
                }
                System.out.println();
            }
            System.out.println();
            System.out.println();
        }
    }
        //获得本地主机和回环地址的基本信息:
        //method_03();

    private static void method_03() throws UnknownHostException {
        InetAddress localhost = InetAddress.getLocalHost();
        System.out.print("localhost.getAddress =\t");
        byte[] bytes1 = localhost.getAddress();
        for (int i = 0 ; i < bytes1.length ; i++){
            System.out.print(bytes1[i]+" ");
        }
        System.out.println();
        System.out.println(localhost.getClass().getName());
        System.out.println("\n");
        InetAddress loop = InetAddress.getLoopbackAddress();
        byte[] byte2 = loop.getAddress();
        System.out.println("loop.getAddress =\t");
        for (int i = 0 ; i < byte2.length ; i++){
            System.out.print(byte2[i]+" ");
        }
        System.out.println();
        System.out.println(loop.getClass().getName());
    }
        //根据主机名获取InetAddress对象(如有多个,获取下标为0的第一个)
        //method_04();

    private static void method_04() throws UnknownHostException {
        //127.0.0.1
        //DESKTOP-0110NQN
        //www.baidu.com
        //localhsot
        InetAddress myAddress1 = InetAddress.getByName("127.0.0.1");
        InetAddress baiduAddress = InetAddress.getByName("www.baidu.com");
        InetAddress myAddress2 = InetAddress.getByName("10.202.133.99");
        InetAddress myAddress3 = InetAddress.getByName("localhost");

        System.out.println(myAddress1.getClass().getName()+":"+myAddress1.getHostAddress());
        System.out.println(baiduAddress.getClass().getName()+":"+baiduAddress.getHostAddress());
        System.out.println(myAddress2.getClass().getName()+":"+myAddress2.getHostAddress());
        System.out.println(myAddress3.getClass().getName()+":"+myAddress3.getHostAddress());
    }
        //根据主机名获取所有的InetAddress对象
        //method_05();

    private static void method_05() throws UnknownHostException {
        InetAddress[] myAddress = InetAddress.getAllByName("DESKTOP-0110NQN");
        InetAddress[] baiduAddress = InetAddress.getAllByName("www.baidu.com");
        InetAddress[] ipStringAddress = InetAddress.getAllByName("10.202.133.99");
        System.out.println("1:");
        for (int i = 0 ; i < myAddress.length ; i++){
            InetAddress inetAddress = myAddress[i];
            System.out.println(inetAddress.getClass().getName()+":"+inetAddress.getHostAddress());
        }
        System.out.println("2:");
        for (int i = 0 ; i < baiduAddress.length ; i++){
            InetAddress inetAddress = baiduAddress[i];
            System.out.println(inetAddress.getClass().getName()+":"+inetAddress.getHostAddress());
        }
        System.out.println("3:");
        for (int i = 0 ; i < ipStringAddress.length ; i++){
            InetAddress inetAddress = ipStringAddress[i];
            System.out.println(inetAddress.getClass().getName()+":"+inetAddress.getHostAddress());
        }
    }
        //根据ip地址获得InetAddress对象
        //method_06();

    private static void method_06() throws Exception{
        byte[] bytes = new byte[]{(byte) -246, (byte) -54, (byte) 133,(byte) 99};
        InetAddress inetAddress = InetAddress.getByAddress(bytes);
        System.out.println(inetAddress.getHostName());
        System.out.println(inetAddress.getHostAddress());
        System.out.println(inetAddress.getClass().getName());
    }
        //根据hostname和address获取InetAddress,hostname只是备注,并不检验是否存在
        //method_07();

    private static void method_07() throws UnknownHostException {
        byte[] bytes = new byte[]{(byte) -246, (byte) -54, (byte) 133,(byte) 99};
        InetAddress inetAddress = InetAddress.getByAddress("test",bytes);
        System.out.println(inetAddress.getHostAddress());
        System.out.println(inetAddress.getHostName());
        System.out.println(inetAddress.getClass().getName());
    }
        //获取权限定主机名和主机名
        method_08();
    private static void method_08() throws Exception{
        InetAddress inetAddress1 = InetAddress.getLocalHost();
        System.out.println(inetAddress1.getHostAddress());
        System.out.println("CanonicalHostName:"+inetAddress1.getCanonicalHostName());
        System.out.println("HostName:"+inetAddress1.getHostName());
        System.out.println("\n");


        InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
        System.out.println(inetAddress2.getHostAddress());
        System.out.println("CanonicalHostName:"+inetAddress2.getCanonicalHostName());
        System.out.println("HostName:"+inetAddress2.getHostName());
        System.out.println("\n");

        InetAddress inetAddress3 =InetAddress.getByName("61.135.169.121");
        System.out.println(inetAddress3.getHostAddress());
        System.out.println("CanonicalHostName:"+inetAddress3.getCanonicalHostName());
        System.out.println("HostName:"+inetAddress3.getHostName());
        System.out.println("\n");
        /*输出:
        CanonicalHostName:DESKTOP-0110NQN
        HostName:DESKTOP-0110NQN

        可以看出,以域名得到的InetAddress,完全限定主机名为ip(这个要看DNS的心情,结果是不确定的),主机名为域名
        CanonicalHostName:61.135.169.125
        HostName:www.baidu.com


        CanonicalHostName:61.135.169.121
        HostName:61.135.169.121
        * */
    }
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值