ちょっと困ったのでメモ
通常JavaのInetAddress.getLocalHost().getHostAddress()から取得できるIPアドレスは127.0.0.1
これではちょっと使えません
ネットワークインターフェースを調べるとちゃんとした?IPアドレスが取得できます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.net.*; import java.util.*; public class test{ public static void main(String[] argv)throws Exception{ Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces(); while (n.hasMoreElements()){ NetworkInterface e = n.nextElement(); Enumeration<InetAddress> a = e.getInetAddresses(); while ( a.hasMoreElements()){ InetAddress addr = a.nextElement(); if (!addr.getHostAddress().equals("127.0.0.1")) System.out.println(addr.getHostAddress()); } } } } |