4

ファイルC:\WINDOWS\system32\drivers\etc\hostsには、次の行しかありません

192.168.0.23    computername.domain.com   computername

私が走るとき

InetAddress localhost = InetAddress.getLocalHost();
System.out.println("getLocalHost:" + localhost);

私は出力が

getLocalHost:computername/192.168.0.23

しかし、それは

getLocalHost:computername/192.168.0.107

なぜこれが起こるのかについてのアイデアはありますか?他のファイルで構成を行う必要がありますか?

編集

InetAddress.getByName('computername')

と同じ IP を生成getLocalHost()します。

4

2 に答える 2

15

getLocalHost()ネットワークアダプタの1つの実際のIPを返します。コマンドラインでipconfigを実行すると、アダプターの1つが同じアドレスを返す必要があります。

複数のアダプタがあり、特定のアダプタが必要な場合はNetworkInterface.getNetworkInterfaces()、各インターフェイスからInetAddressesのリストを使用してプルする必要があります。

于 2011-04-28T03:25:09.393 に答える
4

ファイルのエントリがhostsの IP アドレスに影響するのはなぜlocalhostですか?

InetAddress.getByName('computername')あなたが期待するIPアドレスを与えるはずです。

于 2011-04-28T03:21:14.040 に答える