5

次の問題があります: ArrayList を作成し、この arraylist にクライアントのすべての IP アドレスを入れます (クライアントにネットワーク カードが 1 枚ある場合は 1 つ、ネットワーク カードが n 個ある PC でクライアントが実行されている場合は n)、ループバックを除くアドレス、ポイント ツー ポイント アドレス、および仮想アドレス。

私はこの方法でこれを行いました:

private static List<String> allIps = new ArrayList<String>();

static {
    Enumeration<NetworkInterface> nets;
    try {

        nets = NetworkInterface.getNetworkInterfaces();


        while(nets.hasMoreElements()) {

            NetworkInterface current = nets.nextElement();

            if ((current.isUp()) && (!current.isPointToPoint()) && (!current.isVirtual()) && (!current.isLoopback())) {
                System.out.println(current.getName());
                Enumeration<InetAddress> ee = current.getInetAddresses();


                    while (ee.hasMoreElements()) {
                        InetAddress i = ee.nextElement();
                        System.out.println(i.getHostAddress());
                        allIps.add(i.getHostAddress());

                    }
            }
        }
    } catch (SocketException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println("List of all IP on this client: "
            + allIps.toString());
    System.out.println("Number of ip: " + allIps.size());

}

うまく機能しているようですが、唯一の問題は、(Eclipse コンソールでの) 出力が次のようになっていることです。

eth0
fe80:0:0:0:20c:29ff:fe15:3dfe%2
192.168.15.135
List of all IP on this client: [fe80:0:0:0:20c:29ff:fe15:3dfe%2, 192.168.15.135]
Number of ip: 2

デバッガーとコンソール出力を使用すると、この場合、存在する唯一のネットワーク インターフェイスeth0 (これで問題ありません) であることが明確に見えますが、このネットワーク インターフェイスでは、id が 2 つの IP アドレスを検出しました (適合するのは IPV6 アドレスです)。 2 つ目は従来の IPV4 アドレスです)

それで、それは私のアドレスリストallIpsの両方に入れました。

IPV6 ではなく、 IPV4アドレスのみを選択してallIpsリストに追加します。それを行うにはどうすればよいですか?InetAddressオブジェクトで IPV4 のみをフィルタリングして選択できますか?

TNX

アンドレア

4

1 に答える 1