프로그래밍 언어/Java Programming
InetAddress class host name으로 정보 얻기
Lurutia
2016. 9. 18. 12:50
InetAddress
개념
- IP주소를 다루기 위한 class
- java.net package에 있는 class
메소드
byte[] getAddress() | IP주소를 Byte배열로 반환한다. |
static InetAddress[] getAllByName(String host) | 도메인명(host)에 지정된 모든 호스트의 IP주소를 배열에 담아 반환한다. (nslookup www.naver.com) |
static InetAddress getByAddress(byte[] addr) | byte배열을 통해 IP주소를 얻는다. |
static InetAddress getByName(String host) | 도메인명(host)을 통해 IP주소를 얻는다. |
String getCanonicalHostName() | FQDN(fully qualified domain name)을 반환한다. |
String getHostAddress() | 호스트의 IP주소를 반환한다. |
String getHostName() | 호스트의 이름을 반환한다. |
static InetAddress getLocalHost() | 지역호스트(내컴퓨터)의 IP주소를 반환한다. |
boolean isMulticastAddress() | IP주소가 멀티캐스트 주소인지 알려준다. |
boolean isLoopbackAddress() | IP주소가 loopback 주소(127.0.0.1)인지 알려준다. |
String toString() | 도메인명과 IP주소를 주도록 작성되어있다. |
사용 예
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main2 {
public static void main(String[] args) throws UnknownHostException {
InetAddress localInetAddress;
InetAddress inetAddress;
InetAddress[] inetAddresses;
inetAddress = InetAddress.getByName("www.naver.com");
System.out.print("호스트 네임 : " + inetAddress.getHostName() + "\n");
System.out.print("호스트 IP주소 : " + inetAddress.getHostAddress() + "\n");
System.out.print(inetAddress.toString() + "\n");
inetAddresses = InetAddress.getAllByName("www.naver.com");
for(InetAddress ia : inetAddresses) {
System.out.print(ia.toString() + "\n");
}
localInetAddress = inetAddress.getLocalHost();
System.out.print("로컬 호스트 네임 : " + localInetAddress.getHostName() + "\n");
System.out.print("로컬 호스트 IP주소 : " + localInetAddress.getHostAddress() + "\n");
}
}
결과