티스토리 뷰
이번 포스트는 InetAddress 를 더 자세히 살펴보려한다.
| byte[] | getAddress()
InetAddress 이 객체 의 원시 IP 주소를 반환합니다 .
|
| static InetAddress[] | getAllByName(String host)
호스트 이름이 주어지면 시스템에 구성된 이름 서비스를 기반으로 IP 주소 배열을 반환합니다.
|
| static InetAddress | getByAddress(byte[] addr)
InetAddress원시 IP 주소가 주어진 객체 를 반환합니다 .
|
| static InetAddress | getByAddress(String host, byte[] addr)
제공된 호스트 이름과 IP 주소를 기반으로 InetAddress를 생성합니다.
|
| static InetAddress | getByName(String host)
호스트 이름이 주어지면 호스트의 IP 주소를 결정합니다.
|
| String | getCanonicalHostName()
이 IP 주소에 대한 정규화된 도메인 이름을 가져옵니다.
|
| String | getHostAddress()
텍스트 표현으로 IP 주소 문자열을 반환합니다.
|
| String | getHostName()
이 IP 주소의 호스트 이름을 가져옵니다.
|
| static InetAddress | getLocalHost()
로컬 호스트의 주소를 반환합니다.
|
| boolean | isReachable(int timeout)
해당 주소에 연결할 수 있는지 테스트합니다.
|
| boolean | isReachable(NetworkInterface netif, int ttl, int timeout)
해당 주소에 연결할 수 있는지 테스트합니다.
|
| boolean | isSiteLocalAddress()
InetAddress가 사이트 로컬 주소인지 확인하는 유틸리티 루틴입니다.
|
| String | toString()
이 IP 주소를 String.
|
출처 : https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html
InetAddress (Java Platform SE 8 )
Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. If there is a security manager, this method first calls its checkConnect method wi
docs.oracle.com
getCanonicalHostName() : 정규화된 도메인의 이름을 가져온다.(FQDN)

출처 : https://www.hostinger.com/tutorials/fqdn
FQDN (Fully Qualified Domain Name): What It Is, Examples, and More
A fully qualified domain name (FQDN) is the complete address of a site or a host that includes the hostname or domain name. Learn how it works here.
www.hostinger.com
요약하면, getCanonicalHostName() g함수는 보다 일관된 호스트를 식별하기 위해 사용한다.
**InetAddress**를 배열로 출력할 때
=> 하나의 호스트 이름이 여러개의 IP주소(로드밸런싱, IPv4 / IPv6 등)를 가진경우
public class Main {
public static void main(String[] args) throws UnknownHostException {
String Hostinfo = getHostInfo();
System.out.println("HOST 정보 : " + "\n" + Hostinfo);
}
public static String getHostInfo(){
StringBuilder result = new StringBuilder();
try {
//getCanonicalHostName() : 전체 주소 도메인 네임
String hostName = InetAddress.getLocalHost().getCanonicalHostName();
//getAllByName() : 파라미터에 해당되는 모든 InetAddress 클래스 리턴
InetAddress[] addresses = InetAddress.getAllByName(hostName);
for (InetAddress address : addresses) {
if (address instanceof Inet4Address!= false) {
String ipv4Addressrs = address.getHostAddress();
result.append("HOST IPv4 = ").append(ipv4Addressrs).append("\n");
} else if (address instanceof Inet6Address) {
String ipv6Addressrs = address.getHostAddress();
result.append("HOST IPv6 = ").append(ipv6Addressrs).append("\n");
}
}
if (addresses.length > 0) {
String Hostname = addresses[0].getHostName();
result.append("Host Name = ").append(Hostname).append("\n");
}
}
catch(UnknownHostException e){
e.printStackTrace();
}
return result.toString();
}
}
해당 소스는 String 형식으로 받기위해 StringBuilder를 사용했다.

InetAddress에서 instanceof를 사용할 수 있을 때 :
- InetAddress: 기본 클래스
- Inet4Address: IPv4 주소 클래스
- Inet6Address: IPv6 주소 클래스
- InetSocketAddress: IP 주소와 포트 번호를 함께 다루는 클래스
이중 IPv4 / IPv6 두가지만 소스에 적용했다.
'Spring > Tip' 카테고리의 다른 글
| 로컬 개발환경 HTTPS 설정하기(Tomcat + mkcert) (0) | 2026.04.20 |
|---|---|
| @Scheduled 스프링 스케쥴 (0) | 2024.07.30 |
| 방어적 프로그래밍 (0) | 2024.07.04 |
| [JavaScript] JSON 객체 console 출력 시 [object Object] 해결법 (0) | 2024.07.03 |
| Java - 서버 호스트 (Host) 정보 (2) | 2024.06.19 |
- Total
- Today
- Yesterday
- Column count doesn't match value count at row 1
- 서버호스트
- 특일정보
- JAR매니패스트
- AI
- dbeaver
- springsecurity
- GitHub
- 게시판
- 명령줄이 너무 깁니다
- Powershell
- securityconfig
- 의존성
- CRUD
- insert
- 백엔드
- dependencies
- session저장
- Spring Security
- kotlin
- InetAddress
- Insert오류
- spring ai
- null
- ubuntu
- Git
- gradle
- MariaDB
- Postman
- jwt
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
