域名解析

2020-02-10 15:54:01 浏览数 (2)

InetAddress类

代码语言:javascript复制
import org.testng.annotations.Test;

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

public class tcp {
    @Test(description = "tcp")
    public void tcpgetbyname(){
         /*
         * TCP全称:Transmission Control Protocol
         * 概念:传输控制协议、网络传输层协议
         * 应用:TCP协议网络程序
         * 优点:保证数据传输时间、
         * */
         try{
             InetAddress address = InetAddress.getByName("www.baidu.com");
             System.out.println("返回指定主机名称的ip地址" address);
         } catch(UnknownHostException e) {
             System.out.println("异常,请检查网络状态");
         }
    }

    @Test
    public void tcpgetlocalhost(){
        try{
            InetAddress address = InetAddress.getLocalHost();
            System.out.println("获取本机ip" address);
        }catch(UnknownHostException e){
            System.out.println("异常");
        }
    }

    @Test
    public void tcpgetallbyname(){
        try{
            InetAddress[] address = InetAddress.getAllByName("www.baidu.com");
            System.out.println(address);
            for (int i = 0;i<address.length;i   ){
                System.out.println("第一组" address[i]);
            };
        }catch(UnknownHostException e){
            System.out.println("异常");
        }
    }

    @Test
    public void tcp(){
        String str = "180.101.49.12";
        String[] ipStr = str.split("\.");
        byte[] bytes = new byte[str.length()];
        for(int i = 0; i < 4;i   ){
            bytes[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);
        }
        try{

            InetAddress address = InetAddress.getByAddress("www.baidu.com",bytes);
            System.out.println("str byte" address);
        }catch(UnknownHostException e){
            System.out.println(str bytes);
            System.out.println("异常");
        }
    }

    @Test
    public void tcpback(){
        InetAddress address = InetAddress.getLoopbackAddress();
        System.out.println("getLoopbackAddress" address);
    }
}

0 人点赞