NetworkInterface
클래스는 네트워크 인터페이스에 관한 일부 정보를 액세스할 수 있게 해준다. 사용자는 NetworkInterface
내의 getNetworkInterfaces()
메소드를 이용하여 설치된 일련의 네트워크에 관한 정보를 얻거나, getByName()
또는 getByInetAddress()
메소드를 통해 특정 네트워크를 룩업할 수 있다. 그런 다음 해당 이름이나 InetAddress
같은 네트워크 인터페이스에 관한 정보를 디스플레이할 수 있다. NetworkInterface
를 이용하여 액세스할 수 있는 정보의 종류를 보려면 J2SE 5.0에서 다음 프로그램, ListNets
를 실행하도록 한다. import java.io.*;전형적인 Microsoft Windows 컴퓨터에서
import java.net.*;
import java.util.*;
public class ListNets {
public static void main(String args[])
throws SocketException {
Enumeration<NetworkInterface> nets =
NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
displayInterfaceInformation(netint);
}
}
private static void displayInterfaceInformation(
NetworkInterface netint) throws SocketException {
System.out.printf(
"Display name: %s%n", netint.getDisplayName());
System.out.printf("Name: %s%n", netint.getName());
Enumeration<InetAddress> inetAddresses =
netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(
inetAddresses)) {
System.out.printf("InetAddress: %s%n", inetAddress);
}
System.out.printf("%n");
}
}
ListNets
프로그램을 실행하면 다음과 같은 출력이 표시되어야 한다. (디스플레이 이름과 주소는 현재의 하드웨어와 설정에 따라 차이가 있을 수 있다.) Display name: MS TCP Loopback interface리눅스 시스템도 이름과 관련해서는 유사한 출력 내용을 표시하지만 디스플레이 이름, 때로는 주소에 차이가 있을 수 있다.
Name: lo
InetAddress: /127.0.0.1
Display name: Intel(R) PRO/100 VE Network Connection -
Packet Scheduler Miniport
Name: eth0
Display name: RCA USB Cable Modem - Packet Scheduler Miniport
Name: eth1
InetAddress: /11.22.33.44
isMCGlobal()
및 isMCSiteLocal()
과 같은 메소드로 각각의 InetAddress
에 관해 얻을 수 있는 정보는 멀티캐스팅과 그 주소 타입, 그리고 네트워크 인터페이스와 더 관련이 있다고 볼 수 있는데, Java SE 6.0에서는 이와 같은 네트워크 인터페이스 관련 정보를 NetworkInterface
클래스로 이용할 수 있다. NetworkInterface
클래스에는 네트워크 인터페이스 계층과 관련한 두 가지 메소드 getParent()
와 getSubInterfaces()
가 포함되어 있다. getParent()
메소드는 인터페이스의 부모 NetworkInterface
를 리턴하는데, 다시 말해 어떤 것이 서브인터페이스인 경우에는 getParent()
가 non-null 값을 리턴하게 된다. 한편, getSubInterfaces()
메소드는 네트워크 인터페이스의 모든 서브 인터페이스를 리턴한다. isUp()
메소드를 이용하면 네트워크 인터페이스의 'up' 상태(즉, 실행중) 여부를 확인할 수 있으며, 또한 네트워크 인터페이스의 종류를 알려주는 메소드도 있다. 이 외에도 isLoopback()
은 네트워크 인터페이스가 루프백 인터페이스인지, 그리고 isPointToPoint()
와 isVirtual()
은 각각 point-to-point 인터페이스 및 가상 인터페이스인지 여부를 알려준다. NetworkInterface
에 대해 가용한 정보의 마지막 항목은 InterfaceAddress
라 불리는 새로운 인터페이스의 List
인데, 이는 해당 주소에 대한 InetAddress
, 브로드캐스트 주소 및 서브넷 마스크를 제공한다. NetworkInterface
강화 기능을 이용하는 ListNets
프로그램의 업데이트 버전이다. import java.io.*;Java SE 6.0에서 업데이트된
import java.net.*;
import java.util.*;
public class ListNets {
private static final Console console = System.console();
public static void main(String args[]) throws
SocketException {
Enumeration<NetworkInterface> nets =
NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
displayInterfaceInformation(netint);
}
}
private static void displayInterfaceInformation(
NetworkInterface netint) throws SocketException {
console.printf("Display name: %s%n",
netint.getDisplayName());
console.printf("Name: %s%n", netint.getName());
Enumeration<InetAddress> inetAddresses =
netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(
inetAddresses)) {
console.printf("InetAddress: %s%n", inetAddress);
}
console.printf("Parent: %s%n", netint.getParent());
console.printf("Up? %s%n", netint.isUp());
console.printf("Loopback? %s%n", netint.isLoopback());
console.printf(
"PointToPoint? %s%n", netint.isPointToPoint());
console.printf(
"Supports multicast? %s%n", netint.isVirtual());
console.printf("Virtual? %s%n", netint.isVirtual());
console.printf("Hardware address: %s%n",
Arrays.toString(netint.getHardwareAddress()));
console.printf("MTU: %s%n", netint.getMTU());
List<InterfaceAddress> interfaceAddresses =
netint.getInterfaceAddresses();
for (InterfaceAddress addr : interfaceAddresses) {
console.printf(
"InterfaceAddress: %s%n", addr.getAddress());
}
console.printf("%n");
Enumeration<NetworkInterface> subInterfaces =
netint.getSubInterfaces();
for (NetworkInterface networkInterface : Collections.list(
subInterfaces)) {
console.printf("%nSubInterface%n");
displayInterfaceInformation(networkInterface);
}
console.printf("%n");
}
}
ListNets
를 실행한다. 이 경우에도 출력은 각 사용자의 시스템 구성에 따라 차이가 날 수 있다. 보안상의 이유로 일부 정보의 액세스가 불가능할 수도 있다는 점에 유의할 것. > java ListNets출력은 네트워크 연결 eth1이 'up' 상태이고, IP 주소 11.22.33.44로 인터넷에 연결되어 있다는 것, 그리고 네트워크 연결 eth0이 down 상태임을 나타낸다. 한편 루프백 인터페이스는 up 상태이다(항상 up으로 되어 있어야 함).
Display name: MS TCP Loopback interface
Name: lo
InetAddress: /127.0.0.1
Parent: null
Up? true
Loopback? true
PointToPoint? false
Supports multicast? false
Virtual? false
Hardware address: null
MTU: 1520
InterfaceAddress: /127.0.0.1
Broadcast Address: /127.255.255.255
Network Prefix Length: 8
Display name: Intel(R) PRO/100 VE Network Connection -
Packet Scheduler Miniport
Name: eth0
Parent: null
Up? false
Loopback? false
PointToPoint? false
Supports multicast? false
Virtual? false
Hardware address: [0, 1, 2, 3, 4, 5]
MTU: 1500
Display name: RCA USB Cable Modem - Packet Scheduler Miniport
Name: eth1
InetAddress: /11.22.33.44
Parent: null
Up? true
Loopback? false
PointToPoint? false
Supports multicast? false
Virtual? false
Hardware address: [0, 2, 3, 4, 5, 6]
MTU: 1500
InterfaceAddress: /11.22.33.44
Broadcast Address: /11.22.33.255
Network Prefix Length: 22
ipconfig
명령어(/all
옵션 추가) 등을 통해 얻게되는 프로그램의 결과와 비교해 봤을 때, 상당한 유사점이 있는 것을 알 수 있다.