티스토리 뷰

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");
    }
}

 

결과

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
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
글 보관함