* NetworkInterface API를 통해 해당 로컬 맥주소 및 IP주소를 가져오는 방법입니다.
* 상대방이 나의 서버로 접근해서 어떤 아이피가 찍혔는지는 확인이 가능하지만
맥주소는 확인이 어렵습니다.
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class MacAddressTest {
public static void main(String[] args) {
System.out.println("맥주소확인 : " + getLocalMacAddress());
}
/**
* 로컬 맥 주소를 가져오는 메소드
*
* Created by 닢향
* http://niphyang.tistory.com
*/
public static String getLocalMacAddress() {
String result = "";
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
String ipValue = ip.getHostAddress();
System.out.println("아이피 확인 : " + ipValue);
String ipValue2 = ip.getHostName();
System.out.println("아이피 확인 : " + ipValue2);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
result = sb.toString();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
return result;
}
}
'JAVA > 개발 TIP' 카테고리의 다른 글
[JAVA] 일급 컬렉션 (First Class Collection) (0) | 2021.04.15 |
---|