2015년 12월 3일 목요일

JAVA GetLocalpAddress class

public class GetLocalIpAddress {
    public static String GetLocalIpAddress() {
        try {
            for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();                
                for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();                    
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();                    
                    }
                }
            }
        } catch (SocketException ex) {
            ex.printStackTrace();        
        }
        return null;    
    }
}

댓글 없음:

댓글 쓰기