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
동영상 회의에 사용되는 특별한 IP 주소: 멀티캐스트 주소
멀티캐스트 주소데이터 패킷을 여러 대의 호스트에 동시에 전송하는 방식브로드캐스트와 유니캐스트의 중간에 위치한 통신 방식네트워크에서 멀티캐스트 그룹에 속한 호스트들에게 데이
standout.tistory.com
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
루프백 주소, 네트워크에서 자기 자신을 가리키는 특별한 주소
루프백 주소네트워크에서 자기 자신을 가리키는 특별한 주소 ( 127.0.0.1, ::1 )이 주소를 사용하면 호스트 자신에 대한 네트워크 통신을 시뮬레이션할 수 있다. 간단한 에코 프로그램 소켓 통신
standout.tistory.com