์ƒˆ์†Œ์‹

Back-End/Java

[Java] ํ˜„์žฌ PC ์˜ IP ์ฃผ์†Œ ๋ฐ›์•„์˜ค๋Š” ๋กœ์ง

  • -

 

 

 

 

String ipaddr = null;
try {

    boolean isLoopBack = true;
    Enumeration<NetworkInterface> en;        
    en = NetworkInterface.getNetworkInterfaces();

     while(en.hasMoreElements()) {
        NetworkInterface ni = en.nextElement();
        if (ni.isLoopback())
            continue;

        Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
        while(inetAddresses.hasMoreElements()) { 
            InetAddress ia = inetAddresses.nextElement();
            if (ia.getHostAddress() != null && ia.getHostAddress().indexOf(".") != -1) {
                ipaddr = ia.getHostAddress();
                System.out.println(ipaddr);
                isLoopBack = false;
                break;
            }
        }
        if (!isLoopBack)
            break;
    }
    System.out.println(" IP  =   " + ipaddr);
} catch (SocketException e) {
    e.printStackTrace();
}
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๐Ÿ‘