본문 바로가기

IT/클래스

InetAddress 클래스

InetAddress  클래스는 ip주소를 저장하기 위한 클래스이다. 

이 클래스는 일반적인 클래스와 달리 생성자가 없다. 대신 InetAddress.getByName() 메소드에 호스트의 이름을 주면 해당 컴퓨터의 IP 주소를 포함하는 InetAddress 클래스의 객체를 생성한다.

이 클래스를 사용할때의 주의 사항은

1. 인터넷 연결.

2. 예외처리를 해주어야 한다.

이 두가지 이다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class InetAddressEx {
 
    public static void main(String[] args) throws UnknownHostException {
 
        InetAddress iaddr = InetAddress.getLocalHost();
        System.out.printf("호스트 이름 : %s \n", iaddr.getHostName());
        System.out.printf("호스트 IP 주소 : %s \n", iaddr.getHostAddress());
        iaddr = InetAddress.getByName("www.oracle.com");
        System.out.printf("호스트 이름 : %s \n", iaddr.getHostName());
        System.out.printf("호스트 IP 주소 : %s \n", iaddr.getHostAddress());
        
        InetAddress sw[] = InetAddress.getAllByName("www.daum.net");
        for(InetAddress temp_sw : sw) {
            System.out.printf("호스트 이름 : %s , ", temp_sw.getHostName());
            System.out.printf("호스트 IP 주소 : %s , ", temp_sw.getHostAddress());    
        }
    }
 
}
 
cs


1
2
3
4
5
호스트 이름 : DESKTOP-0DH4OI0 
호스트 IP 주소 : 169.254.61.237 
호스트 이름 : www.oracle.com 
호스트 IP 주소 : 23.50.18.165 
호스트 이름 : www.daum.net , 호스트 IP 주소 : 211.231.99.80 , 호스트 이름 : www.daum.net , 호스트 IP 주소 : 203.133.167.16 , 
cs


'IT > 클래스' 카테고리의 다른 글

RandomAccessFile 클래스  (0) 2018.05.25
[Java]RandomAccessFile 사용하기  (0) 2018.05.24
InputStream 클래스  (0) 2018.05.23
URL 클래스  (0) 2018.05.22
Dimension 클래스  (0) 2018.05.12