本文整理汇总了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;
}
示例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()]);
}
示例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);
}