当前位置: 首页>>代码示例>>Java>>正文


Java PcapNetworkInterface.getAddresses方法代码示例

本文整理汇总了Java中org.pcap4j.core.PcapNetworkInterface.getAddresses方法的典型用法代码示例。如果您正苦于以下问题:Java PcapNetworkInterface.getAddresses方法的具体用法?Java PcapNetworkInterface.getAddresses怎么用?Java PcapNetworkInterface.getAddresses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.pcap4j.core.PcapNetworkInterface的用法示例。


在下文中一共展示了PcapNetworkInterface.getAddresses方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: _selectSuitableNetworkInterface

import org.pcap4j.core.PcapNetworkInterface; //导入方法依赖的package包/类
/**
 * select a most suitable network interface according to the address
 *
 * @param address
 * @return
 */
private SelectedInterface _selectSuitableNetworkInterface(InetAddress address) {
    int similiarBytes = Integer.MIN_VALUE;
    SelectedInterface selectedInterface = new SelectedInterface();

    byte[] inputIpInBytes = address.getAddress();
    for (PcapNetworkInterface currentInterface : _localPcapNetworkInterfaces) {
        List<PcapAddress> addresses = currentInterface.getAddresses();
        if (addresses != null) {
            for (PcapAddress ipAddress : addresses) {
                // make sure the address should be same type, all ipv4 or all ipv6
                if (!_isSameTypeAddress(address, ipAddress.getAddress())) {
                    continue;
                }
                byte[] ipInBytes = ipAddress.getAddress().getAddress();
                int currentSimiliarBytes = _similarBytes(inputIpInBytes, ipInBytes);
                if (currentSimiliarBytes > similiarBytes) {
                    selectedInterface._selectedNetworkInterface = currentInterface;
                    selectedInterface._selectedIpAddress = ipAddress.getAddress();
                    similiarBytes = currentSimiliarBytes;
                }
            }
        }
    }
    return selectedInterface;
}
 
开发者ID:gaoxingliang,项目名称:mac-address-detector-java,代码行数:32,代码来源:MacAddressHelper.java

示例2: toInterfaceAddresses

import org.pcap4j.core.PcapNetworkInterface; //导入方法依赖的package包/类
@Nonnull
private static InterfaceAddress[] toInterfaceAddresses(@Nonnull PcapNetworkInterface iface) {
    Preconditions.checkNotNull(iface, "PcapNetworkInterface was null.");
    List<InterfaceAddress> out = new ArrayList<InterfaceAddress>();
    for (PcapAddress address : iface.getAddresses())
        out.add(toInterfaceAddress(address));
    return out.toArray(new InterfaceAddress[out.size()]);
}
 
开发者ID:shevek,项目名称:dhcp4j,代码行数:9,代码来源:DhcpServer.java

示例3: getLocalAddr

import org.pcap4j.core.PcapNetworkInterface; //导入方法依赖的package包/类
public static void getLocalAddr() throws InterruptedException, PcapNativeException, UnknownHostException, SocketException,
		ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
	if (Settings.getDouble("autoload", 0) == 1) {
		addr = InetAddress.getByName(Settings.get("addr", ""));
		return;
	}

	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	final JFrame frame = new JFrame("Network Device");
	frame.setFocusableWindowState(true);

	final JLabel ipLab = new JLabel("Select LAN IP obtained from Network Settings:", JLabel.LEFT);
	final JComboBox<String> lanIP = new JComboBox<String>();
	final JLabel lanLabel = new JLabel("If your device IP isn't in the dropdown, provide it below.");
	final JTextField lanText = new JTextField(Settings.get("addr", ""));

	ArrayList<InetAddress> inets = new ArrayList<InetAddress>();

	for (PcapNetworkInterface i : Pcaps.findAllDevs()) {
		for (PcapAddress x : i.getAddresses()) {
			InetAddress xAddr = x.getAddress();
			if (xAddr != null && x.getNetmask() != null && !xAddr.toString().equals("/0.0.0.0")) {
				NetworkInterface inf = NetworkInterface.getByInetAddress(x.getAddress());
				if (inf != null && inf.isUp() && !inf.isVirtual()) {
					inets.add(xAddr);
					lanIP.addItem((lanIP.getItemCount() + 1) + " - " + inf.getDisplayName() + " ::: " + xAddr.getHostAddress());
					System.out.println("Found: " + lanIP.getItemCount() + " - " + inf.getDisplayName() + " ::: " + xAddr.getHostAddress());
				}
			}
		}
	}

	if (lanIP.getItemCount() == 0) {
		JOptionPane.showMessageDialog(null, "Unable to locate devices.\nPlease try running the program in Admin Mode.\nIf this does not work, you may need to reboot your computer.",
				"Error", JOptionPane.ERROR_MESSAGE);
		System.exit(1);
	}
	lanIP.setFocusable(false);
	final JButton start = new JButton("Start");
	start.addActionListener(e -> {
		try {
			if (lanText.getText().length() >= 7 && !lanText.getText().equals("0.0.0.0")) { // 7 is because the minimum field is 0.0.0.0
				addr = InetAddress.getByName(lanText.getText());
				System.out.println("Using IP from textfield: " + lanText.getText());
			} else {
				addr = inets.get(lanIP.getSelectedIndex());
				System.out.println("Using device from dropdown: " + lanIP.getSelectedItem());
			}
			Settings.set("addr", addr.getHostAddress().replaceAll("/", ""));
			frame.setVisible(false);
			frame.dispose();
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
	});

	frame.setLayout(new GridLayout(5, 1));
	frame.add(ipLab);
	frame.add(lanIP);
	frame.add(lanLabel);
	frame.add(lanText);
	frame.add(start);
	frame.setAlwaysOnTop(true);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	while (frame.isVisible())
		Thread.sleep(10);
}
 
开发者ID:PsiLupan,项目名称:MakeLobbiesGreatAgain,代码行数:72,代码来源:Boot.java


注:本文中的org.pcap4j.core.PcapNetworkInterface.getAddresses方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。