如何在双栈操作系统上同时获取 IPv4 和 IPv6

How to fetch both IPv4 and IPv6 on dual stack OS(如何在双栈操作系统上同时获取 IPv4 和 IPv6)

本文介绍了如何在双栈操作系统上同时获取 IPv4 和 IPv6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有双栈机器.

我的问题是我只能使用 IPv4

My problem is i am getting only IPv4 using

InetAddress address = InetAddress.getLocalHost();

如果我使用网络接口 API,那么我将获得所有 IP 地址,其中包括我的 MAC 地址以及 IP 地址的形式.why-do-i-get-multiple-global-ipv6-addresses-listed-in-ifconfig

and If i use Network Interface API then i get all the IP address in which includes my MAC addrres as well in the form of IP address. why-do-i-get-multiple-global-ipv6-addresses-listed-in-ifconfig

现在有什么方法可以让我的机器同时获得 IPv4 和 IPv6.

Now is there any way i can get both IPv4 and IPv6 of my machine.

推荐答案

在Linux中,InetAddress.getLocalHost() 将查找主机名,然后返回 DNS 分配给该主机名的第一个 IP 地址.如果您在文件/etc/hosts 中有该主机名,它将获取该文件中该主机名的第一个 IP 地址.

In Linux, InetAddress.getLocalHost() will look for the hostname and then return the first IP address assigned to that hostname by DNS. If you have that hostname in the file /etc/hosts, it will get the first IP address in that file for that hostname.

如果你注意这个方法只返回一个 InetAddress.

If you pay attention this method returns only one InetAddress.

如果您没有分配主机名,很可能是 localhost.localdomain.您可以使用命令行设置主机名:

If you haven't assigned a hostname, most probably it will be localhost.localdomain. You can set the hostname with command line:

hostname [name]

或通过在文件/etc/sysconfig/network 中设置它

or by setting it in file /etc/sysconfig/network

如果您想获取分配给主机名的所有 IP 地址,包括 IPv6,您可以使用:

If you want to get all ip addresses, including IPv6, assigned to a hostname you can use:

InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());

如果要获取分配给主机网络接口的所有 IP 地址,包括 IPv6,则必须使用类 NetworkInterface.

If you want to get all ip addresses, including IPv6, assigned to a host's network interfaces, you must use class NetworkInterface.

我在这里粘贴一些示例代码:

Here I'm pasting some example code:

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.SocketException;
import java.net.NetworkInterface;
import java.util.*;

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            System.out.println("getLocalHost: " + InetAddress.getLocalHost().toString());

            System.out.println("All addresses for local host:");
            InetAddress[] addr = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
            for(InetAddress a : addr)
            {
              System.out.println(a.toString());
            }
        }
        catch(UnknownHostException _e)
        {
            _e.printStackTrace();
        }

        try
        {
            Enumeration nicEnum = NetworkInterface.getNetworkInterfaces();
            while(nicEnum.hasMoreElements())
            {
                NetworkInterface ni=(NetworkInterface) nicEnum.nextElement();
                System.out.println("Name: " + ni.getDisplayName());
                System.out.println("Name: " + ni.getName());
                Enumeration addrEnum = ni.getInetAddresses();
                while(addrEnum.hasMoreElements()) 
                {
                    InetAddress ia= (InetAddress) addrEnum.nextElement();
                    System.out.println(ia.getHostAddress());
                }
            }
        }
        catch(SocketException _e)
        {
            _e.printStackTrace();
        }
    }
}

对于这个例子,我从 InetAddress.getLocalHost().getHostAddress() 正在返回 127.0.1.1

这篇关于如何在双栈操作系统上同时获取 IPv4 和 IPv6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在双栈操作系统上同时获取 IPv4 和 IPv6

基础教程推荐