Socket不是协议,是一种实现计算机之间通信的技术,而HTTP才是协议。
3.1 NetworkInterface类的常用方法
想要取得NetworkInterface对象,就必须要通过NetworkInterface类的public static Enumeration <NetworkInterface >getNetworkInterfaces()方法,该方法的返回值是泛型Enumeration,作用是返回此机器上的所有接口。
3.1.1 获得网络接口的基本信息
public String getName():获得网络设备在操作系统中的名称。该名称并不能得知具体设备的相关信息,仅仅就是一个代号。
public String getDisplayName():取得设备在操作系统中的显示名称。此方法返回的字符串包含厂商名称和网卡具体型号等相关信息。
public int getIndex()方法的作用:获得网络接口的索引。
public boolean isUp()方法的作用:判断网络接口是否已经开启并正常工作。
public boolean isLoopback() 方法的作用:判断网络接口是否为localhost回调/回环接口。
在学习Socket技术时,需要留意一个知识点,就是localhost和127.0.0.1的区别。其实localhost只是一个域名,只有把域名localhost解析为127.0.0.1,才能进行数据传输与通信,这个解析过程是由hosts文件完成的。
public class NetworkInterfaceTest {
public static void main(String[] args) {
try {
Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();
while(networkInterface.hasMoreElements()) {
NetworkInterface eachNetworkInterface = networkInterface.nextElement();
System.out.println("getName获得网络设备名:"+eachNetworkInterface.getName());
System.out.println("getDisplayName获得网络设备显示名称:"+eachNetworkInterface.getDisplayName());
System.out.println("getIndex获得网络接口的索引:"+eachNetworkInterface.getIndex());
System.out.println("isUp判断设备是否已经开启并运行"+eachNetworkInterface.isUp());
System.out.println("isLoopback是否为回调接口="+eachNetworkInterface.isLoopback());
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
运行结果:
getName获得网络设备名:lo
getDisplayName获得网络设备显示名称:Software Loopback Interface 1
getIndex获得网络接口的索引:1
isUp判断设备是否已经开启并运行true
isLoopback是否为回调接口=true
getName获得网络设备名:eth10
getDisplayName获得网络设备显示名称:WAN Miniport (IP)-QoS Packet Scheduler-0000
getIndex获得网络接口的索引:19
isUp判断设备是否已经开启并运行false
isLoopback是否为回调接口=false
getName获得网络设备名:eth11
getDisplayName获得网络设备显示名称:WAN Miniport (IPv6)-QoS Packet Scheduler-0000
getIndex获得网络接口的索引:20
isUp判断设备是否已经开启并运行false
isLoopback是否为回调接口=false
getName获得网络设备名:ppp2
getDisplayName获得网络设备显示名称:我的翼讯
getIndex获得网络接口的索引:21
isUp判断设备是否已经开启并运行true
isLoopback是否为回调接口=false
从上面输出的结果可以得出如下4个结论:
1.网络设备的索引有可能不连续
2.isLoopback()方法针对某一个方法返回的是true,针对其他方法返回的是false,因为系统中只有一个回调/回环地址。
3.isUp()返回true说明设备正在工作。
3.1.2 获取MTU大小
public int getMTU()方法的作用:返回MTU大小。在网络传输中是以数据包为基本传输单位,
可以用MTU(最大传输单元)来规定网络传输最大数据包的大小,单位为字节。
3.1.3 子接口的处理
public Enumeration <NetworkInterface> getSubInterfaces()方法的作用:取得子接口。子接口的作用是在不添加新的物理网卡的基础上,基于原有的网络接口设备在创建出一个虚拟的网络接口设备进行通信,这个虚拟的网络接口可以理解成一个由软件模拟的网卡。windows操作系统不支持子接口,而Linux支持。
public boolean isVirtual()方法的作用:判断当前的网络接口是否为“虚拟子接口”。在Linux操作系统上,虚拟子接口作为物理接口的子接口被创建,并给予不同的设置。
public NetworkInterface getParent()方法的作用:获得父接口。一个虚拟的子网络接口必须依赖于父网络接口。
3.1.4 获得硬件地址
public byte[] getHardwareAddress()方法的作用:获得网卡的硬件地址。
3.1.5 获得IP地址
public Enumeration <InetAddress> getInetAddresses()方法的作用:获得绑定到此网络接口的InetAddress列表,此方法返回泛型Enumeration<InetAddress>
InetAddress类可以表示成互联网协议(IP)地址,通过使用InetAddress对象中的若干方法来获得该IP地址相关信息。一个网络接口可以使用多个IP地址。
1.获得IP地址的基本信息
InetAddress类的常用方法
getCanonicalHostName()方法获取此IP地址的完全限定域名。
getHostName()方法获取此IP地址的主机名
getHostAddress()方法返回IP地址字符串
getAddress()方法返回此InetAddress对象的原始IP地址,返回值是byte[]数组。
static InetAddress getLocalHost()方法的作用:返回本机的IP地址,多个ip返回下标为【0】的第一个IP.
InetAddress[] getAllByName(String host) 给定主机名,返回所有IP地址
static InetAddress getByName(String host):在给定主机名的情况下确定主机的IP地址。
3.1.6 InterfaceAddress类的使用
NetworkInterface:
public java.util.List<InterfaceAddress> getInterfaceAddresses()方法的作用:获得网络接口的InterfaceAddresses列表。
InterfaceAddress:
public InetAddress getAddress()方法的作用:返回此InterFaceAddress的InetAddress.
public InetAddress getBroadcast()方法的作用:返回此InterfaceAddress广播地址的InetAddress.
public short getNetworkPrefixLength()方法的作用:返回此InterfaceAddress的网络前缀长度。
NetworkInterface InterfaceAddress InetAddress这三者之间的关系:
每一个NetworkInterface 有多个InterfaceAddress对象
每个InterfaceAddress对象只有一个InetAddress对象。
3.1.7 判断是否为点对点设备
public boolean isPointToPoint()方法的作用:判断当前的网络设备是不是点对点设备
3.1.8 是否支持多播
public boolean supportsMulticast()方法的作用:判断当前的网络设备是否支持多播。
3.2 NetworkInterface 类的静态方法
public static NetworkInterface getByIndex(int index) 方法的作用:根据指定的索引取得NetworkInterface对象
public static NetworkInterface getByName(String name):根据指定的NetworkInterface的name名称来获取NetworkInterface对象
public static NetworkInterface getByInetAddress(InetAddress addr) 根据指定的InetAddress对象获得NetworkInterface.如果指定的IP地址绑定到多个网络接口,则不确定返回哪个网络接口。