InetAddress
네트워크상의 호스트에 대한 IP 주소를 나타내는 클래스.
byte[] getAddress()
InetAddress의 IP 주소를 byte 배열로 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.example.com");
byte[] ipAddress = address.getAddress();
for (byte b : ipAddress) {
System.out.print((b & 0xFF) + ".");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
93.184.216.34.
static InetAddress[] getAllByName(String host)
주어진 호스트 이름에 대한 모든 IP 주소를 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress addr : addresses) {
System.out.println(addr);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
www.google.com/172.217.3.100
www.google.com/2607:f8b0:4026:805::2004
static InetAddress getByAddress(byte[] addr)
주어진 IP 주소에 대한 InetAddress를 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
byte[] ipAddress = {93, (byte)184, (byte)216, 34};
InetAddress address = InetAddress.getByAddress(ipAddress);
System.out.println(address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
/93.184.216.34
static InetAddress getByName(String host)
주어진 호스트 이름에 대한 InetAddress를 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println(address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
www.example.com/93.184.216.34
String getCanonicalHostName()
InetAddress의 정규화된 호스트 이름을 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.google.com");
System.out.println(address.getCanonicalHostName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
lga25s76-in-x04.1e100.net
String getHostAddress()
InetAddress의 IP 주소를 문자열로 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println(address.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
93.184.216.34
String getHostName()
InetAddress의 호스트 이름을 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("93.184.216.34");
System.out.println(address.getHostName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
www.example.com
static InetAddress getLocalHost()
로컬 호스트의 InetAddress를 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
hostname/127.0.1.1
boolean isMulticastAddress()
InetAddress가 멀티캐스트 주소인지 여부를 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("224.0.0.1");
System.out.println(address.isMulticastAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
true
https://standout.tistory.com/1518
boolean isLoopbackAddress()
InetAddress가 루프백 주소인지 여부를 반환
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("127.0.0.1");
System.out.println(address.isLoopbackAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
[출력결과]
true
https://standout.tistory.com/1517