SSISO Community

시소당

네트워크 파라미터에 대한 프로그램적 액세스

Java SE 6.0(코드명 Mustang)은 빠른 시일 내에 Beta 2 버전을 선보일 예정이며, Java SE 6에 새로 추가되는 기능 중 하나는 네트워크 인터페이스에 관해 이전보다 더 많은 정보를 액세스할 수 있게 해준다는 것이다. 이제 유선, 802.11 a/b/g 무선, 블루투스 등과 같은 복수의 액티브 네트워크 연결을 통해 시스템을 운영하는 것이 그다지 드문 일은 아닌데, 이전 버전의 J2SE의 경우에는 복수 연결과 관련된 정보의 액세스 및 검색에 대한 지원이 제한된 반면 Java SE 6에서는 이 기능을 어느 정도 개선하고 있다.

J2SE 1.4에서 소개된 NetworkInterface 클래스는 네트워크 인터페이스에 관한 일부 정보를 액세스할 수 있게 해준다. 사용자는 NetworkInterface 내의 getNetworkInterfaces() 메소드를 이용하여 설치된 일련의 네트워크에 관한 정보를 얻거나, getByName() 또는 getByInetAddress() 메소드를 통해 특정 네트워크를 룩업할 수 있다. 그런 다음 해당 이름이나 InetAddress 같은 네트워크 인터페이스에 관한 정보를 디스플레이할 수 있다. NetworkInterface를 이용하여 액세스할 수 있는 정보의 종류를 보려면 J2SE 5.0에서 다음 프로그램, ListNets를 실행하도록 한다.
   import java.io.*;
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");
}
}
전형적인 Microsoft Windows 컴퓨터에서 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 클래스로 이용할 수 있다.

네트워크 인터페이스는 계층적으로 조직될 수 있으며, Java SE 6.0의 NetworkInterface 클래스에는 네트워크 인터페이스 계층과 관련한 두 가지 메소드 getParent()getSubInterfaces()가 포함되어 있다. getParent() 메소드는 인터페이스의 부모 NetworkInterface를 리턴하는데, 다시 말해 어떤 것이 서브인터페이스인 경우에는 getParent()가 non-null 값을 리턴하게 된다. 한편, getSubInterfaces() 메소드는 네트워크 인터페이스의 모든 서브 인터페이스를 리턴한다.

isUp() 메소드를 이용하면 네트워크 인터페이스의 'up' 상태(즉, 실행중) 여부를 확인할 수 있으며, 또한 네트워크 인터페이스의 종류를 알려주는 메소드도 있다. 이 외에도 isLoopback()은 네트워크 인터페이스가 루프백 인터페이스인지, 그리고 isPointToPoint()isVirtual()은 각각 point-to-point 인터페이스 및 가상 인터페이스인지 여부를 알려준다.

기 본 상태 정보 외에도, 사용자는 물리적 하드웨어 주소(바이트 어레이로)나 MTU(Maximum Transmission Unit)(최대 패킷 사이즈) 같은 네트워크 인터페이스에 관한 그 밖의 네트워크 파라미터에 액세스할 수 있다.

각각의 NetworkInterface에 대해 가용한 정보의 마지막 항목은 InterfaceAddress라 불리는 새로운 인터페이스의 List인데, 이는 해당 주소에 대한 InetAddress, 브로드캐스트 주소 및 서브넷 마스크를 제공한다.

다음은 NetworkInterface 강화 기능을 이용하는 ListNets 프로그램의 업데이트 버전이다.
   import java.io.*;
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");
}
}
Java SE 6.0에서 업데이트된 ListNets를 실행한다. 이 경우에도 출력은 각 사용자의 시스템 구성에 따라 차이가 날 수 있다. 보안상의 이유로 일부 정보의 액세스가 불가능할 수도 있다는 점에 유의할 것.
   > java ListNets   
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
출력은 네트워크 연결 eth1이 'up' 상태이고, IP 주소 11.22.33.44로 인터넷에 연결되어 있다는 것, 그리고 네트워크 연결 eth0이 down 상태임을 나타낸다. 한편 루프백 인터페이스는 up 상태이다(항상 up으로 되어 있어야 함).

ipconfig 명령어(/all 옵션 추가) 등을 통해 얻게되는 프로그램의 결과와 비교해 봤을 때, 상당한 유사점이 있는 것을 알 수 있다.

Java 플랫폼에서의 네트워크 프로그래밍에 관한 자세한 내용은 자바 튜토리얼의 Custom Networking trail을 참조할 것.

출처 : http://www.sdnkorea.com/blog/159

735 view

4.0 stars