Added helper method to get local IP addresses
This commit is contained in:
parent
93d17f4656
commit
a5052cc917
1 changed files with 28 additions and 0 deletions
|
|
@ -4,7 +4,14 @@ import zutil.osal.MultiCommandExecutor;
|
|||
import zutil.osal.OSAbstractionLayer;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class is a IPv4 scanner, it will scan a
|
||||
|
|
@ -103,9 +110,30 @@ public class InetScanner {
|
|||
return line.contains("TTL=") || line.contains("ttl=");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of IPv4 addresses for the all local network cards
|
||||
*/
|
||||
public static List<InetAddress> getLocalInet4Address(){
|
||||
ArrayList<InetAddress> ips = new ArrayList<>();
|
||||
try {
|
||||
Enumeration<NetworkInterface> netIntf = NetworkInterface.getNetworkInterfaces();
|
||||
while(netIntf.hasMoreElements()){
|
||||
Enumeration<InetAddress> addresses = netIntf.nextElement().getInetAddresses();
|
||||
while (addresses.hasMoreElements()){
|
||||
InetAddress ip = addresses.nextElement();
|
||||
if (ip instanceof Inet4Address && ip.isSiteLocalAddress())
|
||||
ips.add(ip);
|
||||
}
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ips;
|
||||
}
|
||||
|
||||
|
||||
public interface InetScanListener {
|
||||
void foundInetAddress(InetAddress ip);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue