The Inet Address Class Boonrit Kidngun The Inet
The Inet. Address Class Boonrit Kidngun
The Inet. Address Class ����������� IP Address , Host Name ������������ Network Ø ����� IPv 4 ��� IPv 6 Ø ����� Local DNS Server Ø ������ import java. net. Inet. Address Ø
Creating New Inet. Address Objects Ø No Constructor Ø �� 3 static Methods public static Inet. Address get. By. Name(String host. Name) throws Unknown. Host. Exception Ø public static Inet. Address[] get. All. By. Name(String host. Name) throws Ø Unknown. Host. Exception Ø public static Inet. Address get. Local. Host( ) throws Unknown. Host. Exception
Inet. Address. get. By. Name ( ) Ø ���� Address ������������ Ø java. net. Inet. Address address = java. net. Inet. Address. get. By. Name("www. oreilly. com"); Ø Inet. Address address = Inet. Address. get. By. Name("www. oreilly. com");
���� Inet. Address. get. By. Nam e ( ) Ø ���� IP Address ������� Domain Name Ø Ø Ø Ø import java. net. *; import javax. swing. *; public class show. IPwww { public static void main (String[] args) { String do. Main. Name=""; try { do. Main. Name=JOption. Pane. show. Input. Dialog("Domain name : "); Inet. Address address = Inet. Address. get. By. Name(do. Main. Name); JOption. Pane. show. Message. Dialog(null, "IP: "+address. get. Host. Address()); } catch (Unknown. Host. Exception ex) { System. out. println("Could not find "+do. Main. Name); } } }
���� Inet. Address. get. By. Nam ( ) ��� Ø ���� Domaine. Name IP Address Ø Ø Ø Ø import java. net. *; import javax. swing. *; public class show. Do. Main. Name { public static void main (String[] args) { String ip=""; try { ip=JOption. Pane. show. Input. Dialog("Input IP Address : "); Inet. Address address = Inet. Address. get. By. Name(ip); System. out. println(address. get. Host. Name()); } catch (Unknown. Host. Exception ex) { System. out. println("Could not find "+ip); } } }
Inet. Address[ ] get. All. By. Name(String host. Name) Ø ���������� Network ������ IP Address Ø ������ IP Address �� Array Ø Inet. Address[] addresses = Inet. Address. get. All. By. Name("www. apple. com");
Inet. Address[ ] get. All. By. Name(String host. Name) Ø Ø Ø Ø ����� IP Address ��� Microsoft. com import java. net. *; public class All. Addresses. Of. Microsoft { public static void main (String[] args) { try { Inet. Address[] addresses = Inet. Address. get. All. By. Name("www. microsoft. com"); for (int i = 0; i < addresses. length; i++) { System. out. println(addresses[i]); } } catch (Unknown. Host. Exception ex) { System. out. println("Could not find www. microsoft. com"); } } }
Inet. Address[ ] get. All. By. Name(String host. Name) Ø ������� Ø www. microsoft. com/63. 211. 66. 123 Ø www. microsoft. com/63. 211. 66. 124 Ø www. microsoft. com/63. 211. 66. 131 Ø www. microsoft. com/63. 211. 66. 117 Ø www. microsoft. com/63. 211. 66. 116 Ø www. microsoft. com/63. 211. 66. 107 Ø www. microsoft. com/63. 211. 66. 118 Ø www. microsoft. com/63. 211. 66. 115 Ø www. microsoft. com/63. 211. 66. 110
public byte[] get. Address ( ) Ø ���� IP Address ������ Byte Array Ø ������� ����������������� 256 Inet. Address address = Inet. Address. get. By. Name("www. fareastern. ac. th"); Ø byte[] ips=address. get. Address(); Ø
public byte[] get. Address ( ) Ø Ø Ø Ø import java. net. *; public class show. Host. Name. Byte. Array{ public static void main (String[] args) { try { Inet. Address address = Inet. Address. get. By. Name("www. fareastern. ac. th"); byte[] ips=address. get. Address(); for(int i=0; i<ips. length; i++){ int unsigned. Byte = ips[i] < 0 ? ips[i] + 256 : ips[i]; System. out. print(unsigned. Byte+". "); } } catch (Unknown. Host. Exception ex) { System. out. println("Could not find "); } } } ������� 203. 146. 43. 135
Inet. Address get. Local. Host( ) Ø Return Inet. Address ���������� Ø Inet. Address me = Inet. Address. get. Local. Host( ); import java. net. *; public class showip. Local { public static void main (String[] args) { try { Inet. Address address = Inet. Address. get. Local. Host( ); System. out. println(address); } catch (Unknown. Host. Exception ex) { System. out. println("Could not find this computer's address. "); } } }
Testing Reachability Ø ��������������� Host ��������������������� � Ø ����� java 1. 5 ������ Ø ���������� thows IOException Ø ����������� Firewall public boolean is. Reachable(int timeout) Ø public boolean is. Reachable(Network. Interface interface, int ttl, int timeout) Ø Ø ������ Network. Interface ��� Network card �����
Testing Reachability Ø Ø Ø import java. net. *; import java. util. *; import javax. swing. *; Ø public class test. Is. Reach { Ø Ø Ø Ø public static void main (String[] args) { try { Inet. Address address = Inet. Address. get. By. Name(“ 192. 168. 0. 2”); long start=new Date(). get. Time(); if(address. is. Reachable(3000)){ long end=new Date(). get. Time()-start; System. out. println(address +" "+end); }else System. out. println("Error "); } catch (Exception ex) { System. out. println("Could not find "+do. Main. Name); } } } //������� /192. 168. 0. 2 1026
Address Types Ø public boolean is. Any. Local. Address( ) public boolean is. Loopback. Address( ) public boolean is. Link. Local. Address( ) public boolean is. Site. Local. Address( ) public boolean is. Multicast. Address( ) public boolean is. MCGlobal( ) public boolean is. MCNode. Local( ) public boolean is. MCLink. Local( ) public boolean is. MCSite. Local( ) public boolean is. MCOrg. Local( )
�������� Address Types Ø Ø import java. net. *; import javax. swing. *; Ø Ø Ø Ø public class IPCharacteristics { public static void main(String[] args) { String ip=""; try { ip=JOption. Pane. show. Input. Dialog("Input IP Address : "); Inet. Address address = Inet. Address. get. By. Name(ip); if (address. is. Any. Local. Address( )) { System. out. println(address + " is a wildcard address. "); } if (address. is. Loopback. Address( )) { System. out. println(address + " is loopback address. "); }
�������� Address Types Ø Ø Ø Ø Ø if (address. is. Link. Local. Address( )) { System. out. println(address + " is a link-local address. "); } else if (address. is. Site. Local. Address( )) { System. out. println(address + " is a site-local address. "); } else { System. out. println(address + " is a global address. "); } if (address. is. Multicast. Address( )) { if (address. is. MCGlobal( )) { System. out. println(address + " is a global multicast address. "); } else if (address. is. MCOrg. Local( )) { System. out. println(address + " is an organization wide multicast address. "); }
Ø Ø Ø Ø Ø Ø �������� Address Types else if (address. is. MCSite. Local( )) { System. out. println(address + " is a site wide multicast address. "); } else if (address. is. MCLink. Local( )) { System. out. println(address + " is a subnet wide multicast address. "); } else if (address. is. MCNode. Local( )) { System. out. println(address + " is an interface-local multicast address. "); } else { System. out. println(address + " is an unknown multicast address type. "); } } else { System. out. println(address + " is a unicast address. "); } } catch (Unknown. Host. Exception ex) { System. err. println("Could not resolve " + args[0]); } } }
public boolean equals(Object o ( Ø ������� Host Name ����� IP �������� Ø Ø Ø Ø Ø import java. net. *; public class Check. Same. IP { public static void main (String args[]) { try { Inet. Address ibiblio = Inet. Address. get. By. Name("www. ibiblio. org"); Inet. Address helios = Inet. Address. get. By. Name("helios. metalab. unc. edu"); if (ibiblio. equals(helios)) { System. out. println ("www. ibiblio. org is the same as helios. metalab. unc. edu"); } else { System. out. println ("www. ibiblio. org is not the same as helios. metalab. unc. edu"); } } catch (Unknown. Host. Exception ex) { System. out. println("Host lookup failed. "); } } }
The Network. Interface Class ������� Network Card ������������ Ø ��� error �� thows Socket. Exception Ø ����� primary Ethernet interface try { Ø Network. Interface ni = Network. Interface. get. By. Name("eth 0"); if (ni == null) { System. err. println("No such interface: eth 0" ); } } catch (Socket. Exception ex) { System. err. println("Could not list sockets. " ); }
The Network. Interface Class Ø Ø ������ Network. Interface������ import java. net. *; Ø Ø Ø Ø Ø import java. util. *; public class find. Network. Interface { public static void main(String[] args) throws Exception { Enumeration interfaces = Network. Interface. get. Network. Interfaces( ); while (interfaces. has. More. Elements( )) { Network. Interface ni = (Network. Interface) interfaces. next. Element( ); System. out. println(ni); } } }
The Network. Interface Class Ø ������� (������� ) Ø name: lo (MS TCP Loopback interface) index: 1 addresses: /127. 0. 0. 1; Ø name: eth 0 (NVIDIA n. Force Networking Controller Packet Scheduler Miniport) index: 2 addresses: name: eth 1 (Broadcom 802. 11 b/g WLAN - Packet Scheduler Miniport) index: 131075 addresses: Ø /192. 168. 108 Ø
public Enumeration get. Inet. Addresses( ) Ø �� 1 Network interface ������ IP Ø ������� IP Address ��� Network interface Network. Interface eth 0 = Network. Interrface. get. By. Name("eth 0"); Enumeration addresses = eth 0. get. Inet. Addresses( ); while (addresses. has. More. Elements( )) { System. out. println(addresses. next. Element( )); }
- Slides: 23