개발하다보면 현재 IP가 공인 IP인지 사설 IP인지 구분해야할 때가 있다.
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LocalAddress {
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (address.isSiteLocalAddress()) {
System.out.println("Site Local Address: " + address.getHostAddress());
} else {
System.out.println("Routeable Address: " + address.getHostAddress());
}
}
}
위와 같이 사용 가능하다. 또는
import com.google.common.net.InetAddresses;
private static boolean isPrivateV4Address(String ip) {
int address = InetAddresses.coerceToInteger(InetAddresses.forString(ip));
return (((address >>> 24) & 0xFF) == 10)
|| ((((address >>> 24) & 0xFF) == 172)
&& ((address >>> 16) & 0xFF) >= 16
&& ((address >>> 16) & 0xFF) <= 31)
|| ((((address >>> 24) & 0xFF) == 192)
&& (((address >>> 16) & 0xFF) == 168));
}
직접 API를 정의해서 사용할 수도 있다.
출처 :
https://stackoverflow.com/questions/9729378/check-whether-the-ipaddress-is-in-private-range/9729432
Check whether the ipAddress is in private range
How would I check to see whether the ip address is in private category ? if(isPrivateIPAddress(ipAddress)) { //do something } Any suggestions will be appreciated. UPDATED ANSWER private sta...
stackoverflow.com
'개발' 카테고리의 다른 글
Android 11: Developer Preview 3 (0) | 2020.05.06 |
---|---|
How to get the DNS Information (0) | 2020.04.01 |
CAS Descramble Sequence (0) | 2020.03.23 |
Unifying Background Task Scheduling on Android (0) | 2020.03.23 |
Google Home 연동 (0) | 2020.03.14 |