基于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.运行结果: