Example: java_net_04.java
// ネットワーク I/F を表示する import java.net.*; import java.util.Enumeration; public class java_net_04 { public static void main(String[] args) { try { // Network Interface のリスト Enumeration enuIfs = NetworkInterface.getNetworkInterfaces(); while (enuIfs.hasMoreElements()) { NetworkInterface ni = (NetworkInterface)enuIfs.nextElement(); System.out.println("Names: " + ni.getName() + " / " + ni.getDisplayName()); byte hwAddr[] = ni.getHardwareAddress(); if (hwAddr != null) { System.out.print(" MAC address: "); for (byte segment: hwAddr) { System.out.printf("%02x ", segment); } System.out.println(); } Enumeration ipaddrs = ni.getInetAddresses(); while (ipaddrs.hasMoreElements()) { InetAddress address = (InetAddress)ipaddrs.nextElement(); System.out.println(" Address: " + address.getHostAddress()); } } } catch (Exception e) { System.err.println(e); return; } } }実行結果
> javac java_net_04.java > java java_net_04 Names: lo / MS TCP Loopback interface Address: 127.0.0.1 Names: eth0 / VMware Virtual Ethernet Adapter for VMnet8 MAC address: xx xx xx xx xx xx Address: 192.168.153.1 Names: eth1 / VMware Virtual Ethernet Adapter for VMnet1 MAC address: xx xx xx xx xx xx Address: 192.168.247.1 Names: eth2 / Intel(R) 82566DM-2 Gigabit Network Connection - ?p?P?b?g ?X?P?W MAC address: xx xx xx xx xx xx Address: 192.168.1.1
0 件のコメント:
コメントを投稿