基于Java的网络端口信息查询和域名IP查询

一、任务说明

1.实现本机网络端口的状态查询

(1)实现本机网络端口的遍历

(2)根据交互输入网络端口编号,输出对应端口的名称、MAC地址、IP地址、状态、是否本地回路地址等信息

2.实现DNS域名查询

根据交互输入的域名,输出对应的所有IP地址,并返回这些地址是否可达

二、代码实现

1.本机网络端口状态查询

要点说明:

(1)Java实现交互输入使用Scanner,需要在最前方声明使用java.util.*;

// 输入整数
Scanner cin = new Scanner(System.in);
int a = cin.nextInt();

//输入字符串
Scanner name = new Scanner(System.in);
String s = name.next();

(2) MAC地址的转换:判断MAC地址是否为空,若为空则不输出,不为空输出成16进制

//macaddress为byte数组类型,为获取的MAC地址
if(macaddress != null){
   System.out.print("Mac地址:");
   for(int i=0;i<macaddress.length;i++){
       if(i == macaddress.length-1){
           System.out.printf("%02X",macaddress[i]);
       }else{
               System.out.printf("%02X-",macaddress[i]);
       }
   }
   System.out.println();
}

完整代码: 

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

public class test2 {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                System.out.println(networkInterface.getIndex() + "\t" + networkInterface.getDisplayName());
            }
            System.out.println("------------------------------------");
            System.out.print("请输入网络端口编号:");
            Scanner cin = new Scanner(System.in);
            int a = cin.nextInt();
            NetworkInterface number = NetworkInterface.getByIndex(a);
            System.out.println("名称:"+number.getDisplayName());
            byte[] macaddress = number.getHardwareAddress();
            if(macaddress != null){
                System.out.print("Mac地址:");
                for(int i=0;i<macaddress.length;i++){
                    if(i == macaddress.length-1){
                        System.out.printf("%02X",macaddress[i]);
                    }else{
                        System.out.printf("%02X-",macaddress[i]);
                    }
                }
                System.out.println();
            }
            System.out.println("IP地址:"+number.getInterfaceAddresses());
            System.out.println("状态:"+number.isUp());

            InetAddress Loopad = InetAddress.getLoopbackAddress();
            if(Loopad == number.getInterfaceAddresses()){
                System.out.println("回路:true");
            }else{
                System.out.println("回路:false");
            }
            
        } catch(SocketException e) {
            e.printStackTrace();
        } catch(Exception e2) {
            e2.printStackTrace();
        }
    }
}

2.DNS域名查询

要点说明:

是否可达函数isReachable(timeout),括号内为超时时间,一般设为1000~2000ms

完整代码:

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

public class test2 {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                System.out.println(networkInterface.getIndex() + "\t" + networkInterface.getDisplayName());
            }
            System.out.print("请输入域名:");
            Scanner name = new Scanner(System.in);
            String s = name.next();
            InetAddress[] ips = InetAddress.getAllByName(s);
            for(InetAddress ip:ips){
                System.out.println("IP:"+ip.getHostAddress());
                System.out.println("是否可达:"+ip.isReachable(1000));
            }
        } catch(SocketException e) {
            e.printStackTrace();
        } catch(Exception e2) {
            e2.printStackTrace();
        }
    }
}

3.运行结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值