Get a local multiple IP address with Java

xiaoxiao2021-03-05  26

After turning out of the API, take it out. . . Especially suitable for multiple IP addresses

import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class GetIP {public static void main (String [] args) throws UnknownHostException, SocketException {System.out.println ( "Host: / t" InetAddress.getLocalHost () "/ n"); Enumeration en = NetworkInterface .getNetworkInterfaces (); Enumeration addresses; while (en .hasMoreElements ()) {NetworkInterface networkinterface = en.nextElement (); System.out.println (networkinterface.getName ()); addresses = networkinterface.getInetAddresses (); while (addresses.hasMoreElements ()) {System.out.println ("/ t" addresses.nextelement (). gethostaddress () "");}}}}

转载请注明原文地址:https://www.9cbs.com/read-35544.html

New Post(0)