Java Utililty Methods InetAddress Create

List of utility methods to do InetAddress Create

Description

The list of methods to do InetAddress Create are organized into topic(s).

Method

StringgetNetworkCIDR(InetAddress addr, short prefixLength)
get Network CIDR
byte[] raw = addr.getAddress();
boolean isV6 = raw.length > 4;
int networkFields = prefixLength / 8;
for (int i = networkFields + 1; i < raw.length; i++) {
    raw[i] = 0x00;
int networkRemainder = prefixLength % 8;
if (networkFields < raw.length) {
...
StringgetNetworkInterface(InetAddress localAddress)
get Network Interface
try {
    for (NetworkInterface ifc : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        if (ifc.isUp()) {
            for (InetAddress addr : Collections.list(ifc.getInetAddresses())) {
                if (addr.equals(localAddress))
                    return ifc.getDisplayName();
} catch (SocketException e) {
return null;
NetworkInterfacegetNetworkInterfaceByIp(InetAddress inetAddress)
get Network Interface By Ip
for (NetworkInterface ifc : getNetworkInterfaces()) {
    if (ifc.isUp()) {
        Enumeration e = ifc.getInetAddresses();
        while (e.hasMoreElements()) {
            InetAddress inetAddrAll = (InetAddress) e.nextElement();
            if (inetAddrAll.equals(inetAddress)) {
                return ifc;
return null;
NetworkInterfacegetNetworkInterfaceForRemoteIPAddress(InetAddress remoteAddress)
get Network Interface For Remote IP Address
byte remoteAddressBytes[] = remoteAddress.getAddress();
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
    NetworkInterface currentInterface = networkInterfaces.nextElement();
    if (currentInterface.isLoopback()) {
        continue;
    for (InterfaceAddress address : currentInterface.getInterfaceAddresses()) {
...
intgetNextAliablePort(InetAddress address, int startport)
get Next Aliable Port
int port = startport;
while (port < 65535) {
    try {
        Socket socket = new Socket(address, port);
        socket.close();
        port++;
    } catch (Exception ex) {
        return port;
...
intgetNextAvailablePort(InetAddress address, int port)
Return a port number that can be used to create a socket on the given address starting with the given port.
ServerSocket socket = getNextServerSocket(address, port, 0);
int retval = -1;
if (socket != null) {
    retval = socket.getLocalPort();
    try {
        socket.close();
    } catch (IOException e) {
return retval;
StringgetOneDotPrefix(InetAddress addr)
get One Dot Prefix
String textualPrefixAddr = addr.getHostAddress();
int firstDot = textualPrefixAddr.indexOf(".");
textualPrefixAddr = textualPrefixAddr.substring(0, firstDot);
return textualPrefixAddr;
longgetPing(InetAddress address, long timeout)
get Ping
if (System.getProperty("os.name").startsWith("Windows")) {
    return pingWindows(address, timeout);
boolean reached = false;
long start = System.currentTimeMillis();
long end = (start - 1);
try {
    if (address.isReachable(Long.valueOf(timeout).intValue())) {
...
ListgetPrivateInetInetAddress()
get Private Inet Inet Address
List<InetAddress> inetAddresses = new LinkedList<InetAddress>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
    NetworkInterface netInterface = e.nextElement();
    if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
        continue;
    for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
        InetAddress inetAddress = address.getAddress();
...
ProtocolFamilygetProtocolFamily(InetAddress address)
get Protocol Family
if (address instanceof Inet4Address) {
    return StandardProtocolFamily.INET;
} else if (address instanceof Inet6Address) {
    return StandardProtocolFamily.INET6;
} else {
    throw new IllegalStateException("Unknown ProtocolFamily");