Learning notes for the jpcap package (3) Getting network interface devices [Nic devices]

Source: Internet
Author: User

Obtain the network interface device [Nic device]

To capture network packets, you must first obtain the network interface device of your computer, which is also called the NIC device. the jpcap package provides us with this [static method]: jpcapcaptor. getdevicelist () is a list of Network Interface Devices, networkinterface class, and network interface class. It encapsulates the corresponding network port information, such as the NIC name, Nic description, and the data link layer of the NIC.

The simple application code is as follows:

/**
* @ (#) Getdevices. Java
* Demo of displaying network interface device information
* @ Author scholar_ii
* @ Version 1.00
*/
Import jpcap .*;
Public class getdevices
{
Public getdevices ()
{

}

Public static void showneworkinterfacedevices ()
{
// Obtain the instance list of the NIC Device
Networkinterface [] devices = jpcapcaptor. Getdevicelist ();
// Cyclically output information of all Nic Device objects
For (INT I = 0; I <devices. length; I ++)
{// Device number, Network Card Name, network card description
System. Out. println (I + ":" + devices [I]. Name + "(" + devices [I]. Description + ")");
// The name and description of the data link layer of the Network Adapter
System. Out. println ("datalink:" + devices [I]. datalink_name + "(" + devices [I]. datalink_description + ")");
// Nic MAC address
System. Out. Print ("MAC address :");
For (byte B: devices [I]. mac_address) // enhanced variant of the for syntax of jdk1.5 or a later version [loop traversal]
// Convert to a hexadecimal String Representation
System. Out. Print (integer. tohexstring (B & 0xff) + ":");
System. Out. println (); // print out its IP address, subnet mask and broadcast address
// Output Nic IP address IPv4 IPv6 subnet address multicast address
For (networkinterfaceaddress A: devices [I]. addresses)
System. Out. println ("Address:" + A. Address + "" + A. subnet + "" + A. Broadcast );}
}

Public static void main (string [] ARGs ){
Showneworkinterfacedevices ();
}
}

Possible running results are as follows (in Windows XP ):

0:/device/npf_genericdialupadapter (adapter for generic dialup and VPN capture)
Datalink: en10mb (Ethernet)
MAC address: 0: 0: 0: 0: 0: 0:
1:/device/NPF _ {B7CE211D-CEA5-4010-9A39-B833BBEC772D} (RealTek rtl8139/810x family Fast Ethernet NIC (Microsoft's packet schednic ))
Datalink: en10mb (Ethernet)
MAC address: 0: E0: 4C: 87: A0: 5A:
Address:/192.168.1.102/255.255.255.0/255.255.255.255

Note: Nic 0 is a virtual network card of the Operating System (sometimes there is a graphical interface code such as jframe code before the above Code). Nic 0 may not be available, in my experiment, we can get 0 and nic in 2000, and the graphic code in XP will not be affected.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.