在網路編程中,獲取本機IP地址和遠端IP地址是很常見的操作。Java也提供了多種方法獲取IP地址。下面從幾個方面來詳細介紹Java獲取IP地址的方法。
一、如何獲取本機IP地址
獲取本機IP地址有多種方法,這裡介紹其中兩種常用方法。
方法一:使用InetAddress類的getLocalHost()方法
InetAddress ip = InetAddress.getLocalHost(); System.out.println("本機IP地址為:" + ip.getHostAddress());
方法二:使用NetworkInterface類的getNetworkInterfaces()方法遍歷所有網路介面,獲取本機IP地址
Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) { continue; } Enumeration addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isLinkLocalAddress() && !address.isLoopbackAddress() && address instanceof Inet4Address) { System.out.println("本機IP地址為:" + address.getHostAddress()); } } }
二、如何獲取遠端IP地址
獲取遠端IP地址也有多種方法,這裡介紹其中兩種常用方法。
方法一:使用Socket類的getInetAddress()方法獲取遠端IP地址
Socket socket = new Socket("www.google.com", 80); InetAddress ip = socket.getInetAddress(); System.out.println("遠端IP地址為:" + ip.getHostAddress());
方法二:使用DatagramSocket類的receive()方法接收UDP包,從中解析得到遠端IP地址
byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramSocket socket = new DatagramSocket(8080); socket.receive(packet); InetAddress ip = packet.getAddress(); System.out.println("遠端IP地址為:" + ip.getHostAddress());
三、如何獲取MAC地址
在一些場景下,需要獲取本機MAC地址。Java中也提供了獲取MAC地址的方法。
方法一:使用InetAddress類的getByAddress()方法和NetworkInterface類的getHardwareAddress()方法獲取MAC地址
InetAddress ip = InetAddress.getLocalHost(); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(ip); byte[] macBytes = networkInterface.getHardwareAddress(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < macBytes.length; i++) { stringBuilder.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? "-" : "")); } System.out.println("MAC地址為:" + stringBuilder.toString());
方法二:使用Jpcap庫獲取MAC地址
NetworkInterface[] devices = JpcapCaptor.getDeviceList(); JpcapCaptor jpcapCaptor = JpcapCaptor.openDevice(devices[0], 65535, true, 20); byte[] macBytes = jpcapCaptor.getDevice().mac_address; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < macBytes.length; i++) { stringBuilder.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? "-" : "")); } System.out.println("MAC地址為:" + stringBuilder.toString());
以上兩種方法都可以獲取MAC地址,但第二種方法需要使用第三方庫Jpcap。
四、如何獲取多個IP地址
在某些場景下,一台機器可能有多個IP地址,如何獲取這些IP地址呢?可以使用NetworkInterface類的getInetAddresses()方法獲取所有IP地址。
Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); Enumeration addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isLinkLocalAddress() && !address.isLoopbackAddress() && address instanceof Inet4Address) { System.out.println("IP地址為:" + address.getHostAddress()); } } }
總結
Java提供了多種獲取IP地址和MAC地址的方法,我們可以根據不同的場景選擇不同的方法。同時,在使用第三方庫獲取MAC地址時需要注意。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/182211.html