Get all IP addresses of this unit

xiaoxiao2021-03-06  63

Get all the IP address classes in the Java.NETP package of all IP addresses in this machine, providing the method getLocalHost () that can get the native IP address, but only the IP address of the first network device can be obtained by this method. But inetaddress provides GetAllByName (String Host), you can get all IP addresses through the host name. Below is the source code for all IP addresses of this unit. / * NetTools.java * Created on 2004-9-29 * Copyright: * / Import java.net.inetaddress;

/ ** * @Author yanpeng * * / public class nettools {

public static String getLocalHostIP () {String ip; try {InetAddress addr = InetAddress.getLocalHost (); ip = addr.getHostAddress ();} catch (Exception ex) {ip = "";} return ip;} public static String getLocalHostName () {String hostName; try {InetAddress addr = InetAddress.getLocalHost (); hostName = addr.getHostName ();} catch (Exception ex) {hostName = "";} return hostName;} public static String [] getAllLocalHostIP () {String [] RET = NULL; try {string hostname = getLocalHostName (); if (Hostname.Length ()> 0) {inetaddress [] addrs = inetaddress.getallbyName (Hostname); if (addrs.length> 0) {RET = new string [addrs.lendth]; for (int i = 0; i

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

New Post(0)