java笔记 - 输出NetworkAddress, InetAddress, NetworkInterface相关的信息

import java.net.*;
import java.util.*;

import org.junit.Test;


public class TestSocket_AddressesAndInterfaces {

	@Test
	public void NetworkInterfaceStaticMethodsExample () throws Exception {
		Enumeration<NetworkInterface> interfaceList = null;
		interfaceList = NetworkInterface.getNetworkInterfaces();
		
		while(interfaceList.hasMoreElements()) {
			NetworkInterface nif = interfaceList.nextElement();
			printInterfaceInfo(nif);
		}//while
	}

	@Test 
	public void InetAddressStaticExample() throws Exception {
		printf("Local host address is \n");
		printInetAddress(InetAddress.getLocalHost());
		printf("Local Loopback address is \n");
		printInetAddress(InetAddress.getLoopbackAddress());
	}
	@Test 
	public void InterfaceAddressStaticExample() throws Exception {
		printf("There's no useful InterfaceAddress found\n");
	}
	@Test
	public void Test_GetHostName() throws Exception {
		String hostName = "sina.com.cn";
		InetAddress inetAddr=InetAddress.getByName(hostName);
		printf("The InetAddress of %s is \n", hostName);
		printInetAddress(inetAddr);
	}
	@Test
	public void Test_GetAddAddressByHostName() throws Exception {
		String hostName = "docs.google.com";
		for (InetAddress inetAddr :InetAddress.getAllByName(hostName)) {
			printf("The InetAddress of %s is \n", hostName);
			printInetAddress(inetAddr);
		}
	}
	public void printInterfaceInfo(NetworkInterface nif) throws Exception {
		printf("Index=%d\n", nif.getIndex());
		printf("Name=%s\n", nif.getName());
		printf("DisplayName=%s\n", nif.getDisplayName());
		printf("MTU=%d\n", nif.getMTU());
		printf("IsLoopback=%s\n", nif.isLoopback());
		printf("IsPointToPoint=%s\n", nif.isPointToPoint());
		printf("IsUp=%s\n", nif.isUp());
		printf("IsVirtual=%s\n", nif.isVirtual());
		Enumeration<InetAddress> inetAddrList = nif.getInetAddresses();
		printf("+ InetAddress List:\n");
		while(inetAddrList.hasMoreElements()) {
			InetAddress inetAddr = inetAddrList.nextElement();
			println("[");
			printInetAddress(inetAddr);
			println("]");
		}//while
		printf("- InetAddress List\n");
		printf("+ Interface Address List:\n");
		List<InterfaceAddress> addrList2 = nif.getInterfaceAddresses();
		for (InterfaceAddress ifAddress : addrList2) {
			printf("Begin of InterfaceAddress{\n");
			printInterfaceAddress(ifAddress);
			println("}");
		}
		printf("- Interface Address List\n");
		println("");
	}
	public void printInterfaceAddress(InterfaceAddress ifAddress) throws Exception {
		println("\t****The Address of the Interface Address is [");
		printInetAddress(ifAddress.getAddress());
		println("]");
		println("\t***The Broadcast Address of the Interface Address is [");
		printInetAddress(ifAddress.getBroadcast());
		println("]");
	}
	

	public void printInetAddress(InetAddress inetAddr) throws Exception{
		if (null == inetAddr) {
			printf("(null)");
			return;
		}
		printf("\tType=%s\n", inetAddr.getClass().getCanonicalName());
		printf("\tInetAddress.CanonicalHostName=%s\n", inetAddr.getCanonicalHostName());		
		printf("\tInetAddress.HostName=%s\n", inetAddr.getHostName());		
		printf("\tInetAddress.HostAddress=%s\n", inetAddr.getHostAddress());		
		printf("\tInetAddress.isAnyLocalAddress=%s\n", inetAddr.isAnyLocalAddress());		
		printf("\tInetAddress.isLinkLocalAddress=%s\n", inetAddr.isLinkLocalAddress());		
		printf("\tInetAddress.isLoopbackAddress=%s\n", inetAddr.isLoopbackAddress());		
		printf("\tInetAddress.isMCGlobal=%s\n", inetAddr.isMCGlobal());		
		printf("\tInetAddress.isMCLinkLocal=%s\n", inetAddr.isMCLinkLocal());		
		printf("\tInetAddress.isMCNodeLocal=%s\n", inetAddr.isMCNodeLocal());		
		printf("\tInetAddress.isMCOrgLocal=%s\n", inetAddr.isMCOrgLocal());		
		printf("\tInetAddress.isMCSiteLocal=%s\n", inetAddr.isMCSiteLocal());		
		printf("\tInetAddress.isMulticastAddress=%s\n", inetAddr.isMulticastAddress());		
		printf("\tInetAddress.isReachable(10)=%s\n", inetAddr.isReachable(10));		
		printf("\tInetAddress.isSiteLocalAddress=%s\n", inetAddr.isSiteLocalAddress());		
	}

	public void printf(String format, Object... args) {
		System.out.print(Thread.currentThread().getId());
		System.out.print('\t');
		if (args == null || args.length == 0) {
			System.out.print(format);
		} else {
			System.out.printf(format, args);
		}
	}
	public void println(String msg) {
		System.out.print(Thread.currentThread().getId());
		System.out.print('\t');
		System.out.println(msg);
	}
}

posted on 2012-03-11 13:22  learner  阅读(954)  评论(0)    收藏  举报

导航