When it comes to acquiring an IP address, one might think of using inetaddress.getlocalhost directly (). Gethostaddress (). In fact, this is wrong, because there may be multiple network interfaces on a machine (usually referred to as a NIC or a virtual network card), so there are multiple IP addresses, so we need to list all the network interface and its corresponding IP address. The code is as follows:
public static void Main (string[] args) throws Exception { enumeration<networkinterface> netinterfaces = Networkinterface.getnetworkinterfaces (); while (Netinterfaces.hasmoreelements ()) { NetworkInterface ni = netinterfaces.nextelement (); SYSTEM.OUT.PRINTLN ("Network Interface Name:" + ni.getdisplayname ()); Enumeration<inetaddress> ia = ni.getinetaddresses (); while (Ia.hasmoreelements ()) { inetaddress IP = ia.nextelement (); System.out.println (Ip.gethostaddress ());}}}
Network interface name: Software Loopback Interface 10:0:0:0:0:0:0:1127.0.0.1 network interface name: WAN Miniport (SSTP) network interface name: Microsoft ISATAP Adapter #6fe80:0:0:0:0:5efe:c0a8:bf01%3 Network Interface Name: Microsoft Kernel Debug network Adapter Networking Interface Name: Microsoft ISATAP Adapter #4fe80:0 : 0:0:0:5efe:c0a8:167%5 Network Interface name: WAN Miniport Network Interface name: Microsoft ISATAP Adapter #5网络接口名称: WAN Miniport (IPV6 ) network interface name: Microsoft ISATAP Adapter #2fe80:0:0:0:0:5efe:c0a8:b501%9 network Interface name: WAN Miniport (IP) network interface name: WAN Miniport (PPPOE) Network interface Name: WAN Miniport (L2TP) networking interface Name: Microsoft Hosted network Virtual adapterfe80:0:0:0:b9b1:54f2:26a3:e07f% 13192.168.191.1 Network Interface Name: Microsoft Teredo tunneling ADAPTER2001:0:5EF5:79FD:C5B:37FD:3F57:FE98FE80:0:0:0:C5B:37FD : 3f57:fe98%14 Network Interface name: VMware Virtual Ethernet Adapter for vmnet8fe80:0:0:0:39ac:b0f8:cb0c:2868% 15192.168.181.1 Network Interface name: Broadcom 802.11íøâçêêåäæ÷ì? Îþï? öóòíøfe80:0:0:0:3cb8:6486:b40b:1cb5%16192.168.1.103 Network Interface Name: VMware Virtual Ethernet Adapter for Vmnet1fe80:0:0:0:2dd6 : 2ca:d599:b57e%17192.168.72.1 Network Interface Name: Microsoft Wi-Fi direcT Virtual adapterfe80:0:0:0:4cad:987a:b4a1:f1bb%18 Network interface name: Realtek PCIe FE Family controllerfe80:0:0:0:dbb:193c:865f: C467%19 Network interface Name: WAN Miniport (PPTP) network interface name: WAN Miniport (IKEV2) network interface name: Microsoft ISATAP Adapter #8fe80:0:0:0:0:5efe:c0a8 : 4801%22 Network Interface name: Remote NDIS based Internet sharing device network interface name: Realtek PCIe FE Family controller-wfp Native MAC Layer Ligh Tweight Filter-0000 Network Interface name: Realtek PCIe fe Family controller-liebao wifi NAT Driver-0000 network interface name: Realtek PCIe fe Family contr Oller-qos Packet Scheduler-0000 Network interface name: Realtek PCIe FE Family CONTROLLER-WFP 802.3 MAC Layer lightweight Filter-0000 Network interface name : Broadcom 802.11íøâçêêåäæ÷ì? Îþï? ÖÓÒÍØ¡£-WFP Native MAC Layer lightweight Filter-00 Network interface name: Broadcom 802.11íøâçêêåäæ÷ì? Îþï? Öóòíø¡£-virtual wifi Filter Driver-00 network interface name: Broadcom 802.11íøâçêêåäæ÷ì? Îþï? Öóòíø¡£-native wifi Filter Driver-00 network interface name: Broadcom 802.11íøâçêêåäæ÷ì? Îþï? Öóòíø¡£-liebao wifi NAT Driver-00 network interface name: Broadcom 802.11íøâçêêåäæ÷ì? Îþï? Öóòíø¡£-qos Packet Scheduler-00 Network interface name: Broadcom 802.11íøâçêÊåäæ÷ì? Îþï? ÖÓÒÍØ¡£-WFP 802.3 mac layer lightweight Filter-00 Network interface name: Microsoft Wi-Fi Direct Virtual ADAPTER-WFP Native MAC layer LIGHTW Eight Filter-0000 Networking interface name: Microsoft Hosted network Virtual ADAPTER-WFP Native MAC Layer Lightweight Filter-0000 Network Interface Name: Microsoft Hosted Networks Virtual adapter-native wifi Filter Driver-0000 network interface name: Microsoft Wi-Fi Direct Virtual adapter-native wifi Filter Driver-0000 network interface name: Microsoft Wi-Fi Direct Virtual Adapter-liebao wifi NAT Driver-0000 Network Interface Name: Microsoft Wi-Fi Direct virtual Adapter-qos Packet Scheduler-0000 Network interface name: Microsoft Wi-Fi Direct virtual ADAPTER-WFP 80 2.3 MAC Layer Lightweight Filter-0000 Network interface name: Microsoft Hosted networks Virtual Adapter-liebao wifi NAT Driver-0000 network interface name: Mi Crosoft Hosted Network virtual Adapter-qos Packet Scheduler-0000 Networking Interface Name: Microsoft Hosted Networks Virtual ADAPTER-WFP 802.3 MAC layer lightweight Filter-0000 network interface name: WAN Miniport (IP)-WFP Native MAC layer Lightweight Filter-0000 Network interface name: WAN Miniport (IP)-liebao wifi NAT Driver-0000 network interface name: WANMiniport (IP)-qos Packet Scheduler-0000 Network interface name: WAN Miniport (IPV6)-WFP Native MAC Layer lightweight Filter-0000 Network interface name: WAN Miniport (IPV6)-liebao wifi NAT Driver-0000 network interface name: WAN Miniport (IPV6)-qos Packet Scheduler-0000 Network interface name: WAN Miniport ( Network Monitor)-WFP Native MAC Layer lightweight Filter-0000 Networking Interface Name: WAN miniport (Network Monitor)-liebao Wifi NAT driver- 0000 Interface Name: WAN Miniport (Network Monitor)-qos Packet Scheduler-0000
such as fe80:0:0:0:0:5efe:c0a8:bf01%3 this is the address of the IPV6 format, a total of 128 bits, each 16 bits separated by semicolons, the last 3% means that the address is limited to the network interface labeled 3, and the other network interface this address is invalid.
Java gets the IP address of this machine