⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nic.java

📁 基于Jpcap开发的流量监控软件
💻 JAVA
字号:
package joel.ethernettools;
/**
 * THIS CLASS REPRESENTS THE NICs INSTALLED ON THE PC.
 * AND PROVIDES INFORMATIONS ABOUT THEM.
 */
import java.net.InetAddress;
import java.net.UnknownHostException;

public class NIC {
	private static int SelectedNICIndex = -1;
	private String Description;
	private byte mac[];
	private InetAddress ip[], subnet[];
	private final static jpcap.NetworkInterface nics[] = jpcap.JpcapCaptor
			.getDeviceList();
	private jpcap.NetworkInterface Interface;
	private MsgProvider msgProvider;

	protected NIC(MsgReceiver logger) {
		msgProvider = new MsgProvider();
		this.msgProvider.addMsgReceiver(logger);
	}

	protected NIC() {

		msgProvider = new MsgProvider();
	}

	protected void setSelectedNIC(int index) {
		SelectedNICIndex = index;
		Interface = nics[index];
		Description = Interface.description;
		ip = new InetAddress[Interface.addresses.length];
		subnet = new InetAddress[Interface.addresses.length];
		mac = Interface.mac_address;
		for (int i = 0; i < ip.length; i++) {
			ip[i] = Interface.addresses[i].address;
			subnet[i] = Interface.addresses[i].subnet;
		}
		msgProvider.addNewMsg("NIC SELECTED: " + this.getNICDescription());
		msgProvider.addNewMsg("MAC: " + this.getMACString());
		for (int i = 0; i < ip.length; i++)
			msgProvider.addNewMsg("IP: " + ip[i].getHostAddress());
	}

	protected static String[] getInterfaceNames() {
		String temp[] = new String[nics.length];
		for (int i = 0; i < nics.length; i++) {
			temp[i] = nics[i].description + " : " + nics[i].name;
		}
		return temp;
	}

	protected InetAddress getIPAddress() {
		return ip[0];
	}

	protected byte[] getMAC() {
		return mac;
	}

	protected String getMACString() {
		return NIC.getMACString(this.mac);
	}

	protected static String getMACString(byte[] macAddr) {
		String mac = "";
		int temp = 0;
		for (int i = 0; i < macAddr.length; i++) {
			temp = Integer.valueOf((int) macAddr[i] & 0xff);
			if (temp <= 15)
				mac += "0";
			mac += Integer.toHexString(temp);
		}
		return mac;
	}

	protected void setMAC(final byte[] mac) {
		this.mac = mac;
	}

	protected String getNICDescription() {
		return this.Description;
	}

	/**
	 * for test only.
	 */
	protected jpcap.NetworkInterface getInterface() {
		return Interface;
	}

	/**
	 * convert ip or mac address to byte[].
	 * 
	 * @param s
	 *            ip or mac address
	 * @return byte[]
	 */
	protected static byte[] getByteArrayByString(String s) {
		byte[] temp;
		try {
			InetAddress addr = InetAddress.getByName(s);
			temp = new byte[4];
			temp = addr.getAddress();
			return temp;
		} catch (UnknownHostException e) {
			temp = new byte[6];
			for (int i = 0; i < temp.length; i++) {
				temp[i] = (byte) Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16);
			}
			return temp;
		}
	}

	public static void main(String s[]) {
		NIC n=new NIC();
		n.setSelectedNIC(1);
		System.out.println(n.getInterface().description);
		System.out.println(n.getInterface().datalink_name);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -