- getAddress() : InetAddress 클래스가 가지고 있는 IP주소의 byte 배열 가져옴

- getHostAddress() : IP주소 문자열로 가져옴

- getHostName() : hostname을 문자열로 나타냄

- getByName(String host) : host의 IP 주소를 가져옴

- getLocalhost() : 사용자의 시스템 IP 주소를 가져옴


import java.net.InetAddress;


public class dailyCode {

public static void main(String[] args) {

try{

String google = "www.google.com";

String naver = "www.naver.com";

InetAddress googleAddr = InetAddress.getByName(google);

String googleName = googleAddr.getHostName();

String googleAddress = googleAddr.getHostAddress();

System.out.println("googleAddr = " + googleAddr);

System.out.println("googleName = " + googleName + ", googleAddress = " + googleAddress);

InetAddress local = InetAddress.getLocalHost();

byte[] localAddr = local.getAddress();

System.out.println("local = " + local);

for(int i=0; i<localAddr.length; i++){

System.out.print((localAddr[i]<0?(localAddr[i]+256):localAddr[i]));

if(i < localAddr.length-1){

System.out.print(".");

}

}

}catch(Exception e) {

e.printStackTrace();

}

}


'프로그래밍 > JAVA' 카테고리의 다른 글

LinkedList Implementation  (0) 2016.11.29
How to get google server time (URLConnection Example)  (0) 2016.11.27
URL class Example  (0) 2016.11.27
람다 표현식 (Lambda Expression)  (0) 2016.11.24
Arrays class Example  (0) 2016.11.23
Hashtable and HashMap Example  (0) 2016.11.21
Generics and Wildcard  (0) 2016.11.20

+ Recent posts