صفحه 1 از 1

چگونگی بدست آوردن IP و MAC آدرس در جاوا

ارسال شده: پنج شنبه 20 دی 1397, 2:10 am
توسط java2

کد: انتخاب همه


	InetAddress ip = InetAddress.getLocalHost();
        System.out.println("IP  : " + ip.getHostAddress());
        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(ip);
        byte[] mac = networkInterface.getHardwareAddress();
        System.out.print("MAC : ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        System.out.println(sb.toString());