首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在java中获取本地ipv6地址

如何在java中获取本地ipv6地址
EN

Stack Overflow用户
提问于 2014-08-05 16:53:59
回答 3查看 1.6K关注 0票数 0

大家好,我想要得到eth0接口的ipv6地址,

例如,我的eth0接口:

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

如何在java中获取inet6地址?

我无法正确使用InetAdress类。它始终返回ipv4地址。

EN

回答 3

Stack Overflow用户

发布于 2014-08-06 17:26:41

谢谢你的建议。我用下面的代码获得了关于接口的所有信息。

此外,InetAddress类在Linux中不能正常工作。(我尝试在linux中获取硬件地址),但代码在windows和linux上工作正常。

代码语言:javascript
复制
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");
 }
 }  

致以问候。

票数 2
EN

Stack Overflow用户

发布于 2018-08-02 17:31:57

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2014-08-05 21:01:58

您可以使用Inet6Address子类请参阅http://docs.oracle.com/javase/6/docs/api/java/net/Inet6Address.html

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25134753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档