Erol Guzoğlu
Erol Guzoğlu

Reputation: 486

How Can Get Local ipv6 adress in java

Hi everyone i want to get ipv6 address of eth0 interface,

for example my eth0 interface:

eth0 : Link encap:Ethernet HWaddr 11:11:11:11:11:11 inet addr:11.11.11.11 Bcast:11.11.11.255 Mask:255.255.255.0 inet6 addr: 1111:1111:1111:1111:1111:1111/64 Scope:Link

How can I get an inet6 address in java?

I cannot use the InetAdress class correctly. It always returns an ipv4 address.

Upvotes: 0

Views: 2793

Answers (3)

Vitaliy Polchuk
Vitaliy Polchuk

Reputation: 1898

private static String getIPAddress(boolean v6) throws SocketException {
    Enumeration<NetworkInterface> netInts = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netInt : Collections.list(netInts)) {
        if (netInt.isUp() && !netInt.isLoopback()) {
            for (InetAddress inetAddress : Collections.list(netInt.getInetAddresses())) {

                if (inetAddress.isLoopbackAddress()
                     || inetAddress.isLinkLocalAddress()
                     || inetAddress.isMulticastAddress()) {
                    continue;
                }

                if (v6 && inetAddress instanceof Inet6Address) {
                    return inetAddress.getHostAddress();
                }

                if (!v6 && inetAddress instanceof InetAddress) {
                    return inetAddress.getHostAddress();
                }

            }
        }
    }
    return null;
}

Upvotes: 1

Erol Guzoğlu
Erol Guzoğlu

Reputation: 486

Thanks for suggestions. I got all of information about the interfaces with the following code.

Also, InetAddress class does not work correctly in Linux. (I tried get hardware adress in linux) But the code work on windows and linux correctly.

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class GetInfo {

public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets))
        displayInterfaceInformation(netint);
}

static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    out.printf("Display name: %s\n", netint.getDisplayName());
    out.printf("Name: %s\n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();

    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        out.printf("InetAddress: %s\n", inetAddress);
    }

    out.printf("Up? %s\n", netint.isUp());
    out.printf("Loopback? %s\n", netint.isLoopback());
    out.printf("PointToPoint? %s\n", netint.isPointToPoint());
    out.printf("Supports multicast? %s\n", netint.supportsMulticast());
    out.printf("Virtual? %s\n", netint.isVirtual());
    out.printf("Hardware address: %s\n",
                Arrays.toString(netint.getHardwareAddress()));
    out.printf("MTU: %s\n", netint.getMTU());
    out.printf("\n");
 }
 }  

Regards.

Upvotes: 3

codelion
codelion

Reputation: 1066

You can use the Inet6Address subclass see http://docs.oracle.com/javase/6/docs/api/java/net/Inet6Address.html

Upvotes: -1

Related Questions