1. java.net.NetworkInterface类的使用
1.3. java.net.NetworkInterface的方法。
NetworkInterface定义了很多获取网络接口信息的方法,其中很多是jdk1.6版本以后才加入进去的。主要有以下方法:
String displayName() 获取网络接口的显示名称
byte[] getHardwareAddress() 获取网络接口的物理地址,通常是MAC地址
int getMTU()返回此接口的最大传输单元(Maximum Transmission Unit,MTU)
String getName()获取此网络接口的名称
boolean isLoopback()返回网络接口是否是回送接口
boolean isPointToPoint()返回网络接口是否是点对点接口
boolean isUp()返回网络接口是否已经开启并运行
boolean isVirtual()返回此接口是否是虚拟接口(也称为子接口)。
下面的例子展示了如何获取本地网络接口以及显示其信息。
package org.dakiler.javanet.chapter1;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class Example6
{
public static void main(String args[])throws Exception
{
Enumeration<NetworkInterface>e=NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
NetworkInterface ni=e.nextElement();
System.out.println("displayname: "+ni.getDisplayName());
System.out.println("name: "+ni.getName());
System.out.println("MTU: "+ni.getMTU());
System.out.println("Loopback: "+ni.isLoopback());
System.out.println("Virtual: "+ni.isVirtual());
System.out.println("Up: "+ni.isUp());
System.out.println("PointToPoint: "+ni.isPointToPoint());
byte[]mac=ni.getHardwareAddress();
if(mac!=null)
displayMac(mac);
else System.out.println("mac is null");
System.out.println("-----");
}
}
public static void displayMac(byte[] mac)
{
for(int i=0;i<mac.length;i++)
{
byte b=mac[i];
int intValue=0;
if(b>=0)intValue=b;
else intValue=256+b;
System.out.print(Integer.toHexString(intValue));
if(i!=mac.length-1)
System.out.print("-");
}
System.out.println();
}
}
1.4. 其他
java.net.NetworkInterface提供了api来访问网络接口的信息,不过都只是获取基本信息,没有提供对数据链路层的控制的api.而且java标准版本都没有提供对网络层以下的控制,需要使用第三方的API。
比较有名的是JPCAP。JPCAP调用wincap/libpcap,而给JAVA语言提供一个公共的 接口,从而实现了平台无关性。这个API提供了在数据链路层发送接收数据包的功能。如果需要了解更多,请参考JPCAP的相关技术文档。
分享到:
相关推荐
本demo是通过NetworkInterface和SharpPcap里面的MAC地址来匹配的,即同一个网口的MAC地址在NetworkInterface和SharpPcap里面都是相同的,通过MAC地址就可以找到SharpPcap里面的对应网口,从而去使用SharpPcap接口来...
Java中通过NetworkInterface获取主机地址和物理地址等;Java中通过NetworkInterface获取主机地址和物理地址等;Java中通过NetworkInterface获取主机地址和物理地址等
Telemecanique Interbus Network Interface Applications Guidepdf,Telemecanique Interbus Network Interface Applications Guide
Telemecanique Fipio Network Interface Applications Guidepdf,Telemecanique Fipio Network Interface Applications Guide
Telemecanique Modbus Plus Network Interface Applications Guidepdf,Telemecanique Modbus Plus Network Interface Applications Guide
Telemecanique DeviceNet Network Interface Applications Guidepdf,Telemecanique DeviceNet Network Interface Applications Guide
Design of Embedded Network Interface Controller 供参考翻译
Hamachi Network Interface虚拟网卡驱动程序,安装时请自选驱动,指定该文件即可,可以安装多块虚拟网卡,在用虚拟机和dynamips时特别有用
Oracle Solaris 8 Network Interface Guide-144
Telemecanique Advantys STB Basic CANopen Network Interface Module Applications Guidepdf,Telemecanique Advantys STB Basic CANopen Network Interface Module Applications Guide
今天介绍一种通用的跨平台的操作方式,那就是JDK自带的NetworkInterface接口,该接口在JDK1.4已经出现,但是功能比较少,JDK1.6之后新增了不少新功能,比较不错
Telemecanique Advantys STB Standard Modbus Plus Network Interface Module Applications Guidepdf,Telemecanique Advantys STB Standard Modbus Plus Network Interface Module Applications Guide
aix5.3 Switch Network Interface for eServer™ pSeries® High Performance Switch Guide
Network Driver Interface Specification,即网络驱动接口规范
java.net.NetworkInterface 类包装器。 用法 按名称获取网络接口: => (by-name "eth3") #<NetworkInterface> 获取网络接口列表作为枚举: => (network-interfaces) # 使用 EnumerationSeq 获取网络接口列表: ...
本资源包含两个文档,一个是最新的Network Controller Sideband Interface(NC-SI)SpecificationV1.1.0官方发布版本,一个是2022年5月的最新版本Network Controller Sideband Interface(NC-SI)SpecificationV1.2WIP90...
原版英文手册。用于PLC做服务器进行MODBUS TCP通信。 Chapter 1 Introduction, Description, and Specifications Chapter 2 Installation Chapter 3 Configuring an Ethernet NIU and I/O Station ...
Serial Port Network Interface Test Server
Lora gateway to network server interface definition