Inet

1,442 views

Published on

PPT

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Inet

  1. 1. The InetAddress Class Boonrit Kidngun
  2. 2. The InetAddress Class <ul><li>ใช้แสดงและจัดการค้นหา IP Address ,Host Name ของเครื่องคอมพิวเตอร์ที่ อยู่ในระบบ Network </li></ul><ul><li>ใช้ได้กับ IPv4 และ IPv6 </li></ul><ul><li>ค้นหาผ่าน Local DNS Server </li></ul><ul><li>การเรียกใช้ import java.net.InetAddress </li></ul>
  3. 3. Creating New InetAddress Objects <ul><li>No Constructor </li></ul><ul><li>มี 3 static Methods </li></ul><ul><li>public static InetAddress getByName ( String hostName ) throws UnknownHostException </li></ul><ul><li>public static InetAddress [] getAllByName ( String hostName ) throws UnknownHostException </li></ul><ul><li>public static InetAddress getLocalHost ( ) throws UnknownHostException </li></ul>
  4. 4. InetAddress . getByName ( ) <ul><li>การค้นหา Address จาก ชื่อเครื่องคอมพิวเตอร์ </li></ul><ul><li>java . net . InetAddress address = java . net . InetAddress . getByName (&quot; www . oreilly . com &quot;) ; </li></ul><ul><li>InetAddress address = InetAddress . getByName (&quot; www . oreilly . com &quot;) ; </li></ul>
  5. 5. ตัวอย่าง InetAddress . getByName ( ) <ul><li>แสดง IP Address จากชื่อ Domain Name </li></ul><ul><li>import java.net.*; </li></ul><ul><li>import javax.swing.*; </li></ul><ul><li>public class showIPwww { </li></ul><ul><li>public static void main (String[] args) { </li></ul><ul><li>String doMainName=&quot;&quot;; </li></ul><ul><li>try { </li></ul><ul><li> doMainName=JOptionPane.showInputDialog(&quot;Domain name :&quot;); </li></ul><ul><li>InetAddress address = InetAddress.getByName(doMainName); </li></ul><ul><li>JOptionPane.showMessageDialog(null,&quot;IP:&quot;+address.getHostAddress()); </li></ul><ul><li>} </li></ul><ul><li>catch (UnknownHostException ex) { </li></ul><ul><li>System.out.println(&quot;Could not find &quot;+doMainName); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  6. 6. ตัวอย่าง InetAddress . getByName ( ) <ul><li>แสดง ชื่อ Domain Name จาก IP Address </li></ul><ul><li>import java . net .* ; </li></ul><ul><li>import javax . swing .* ; </li></ul><ul><li>public class showDoMainName { </li></ul><ul><li>public static void main ( String [] args ) { </li></ul><ul><li>String ip =&quot;&quot; ; </li></ul><ul><li>try { </li></ul><ul><li>ip = JOptionPane . showInputDialog (&quot; Input IP Address :&quot;) ; </li></ul><ul><li>InetAddress address = InetAddress . getByName ( ip ) ; </li></ul><ul><li>System . out . println ( address . getHostName ()) ; </li></ul><ul><li>} </li></ul><ul><li>catch ( UnknownHostException ex ) { </li></ul><ul><li>System . out . println (&quot; Could not find &quot;+ ip ) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. InetAddress [ ] getAllByName ( String hostName ) <ul><li>คอมพิวเตอร์ที่อยู่บน Network อาจจะมีหลาย IP Address </li></ul><ul><li>ทำเก็บ IP Address ใน Array </li></ul><ul><li>InetAddress [] addresses = InetAddress . getAllByName (&quot; www . apple . com &quot;) ; </li></ul>
  8. 8. InetAddress [ ] getAllByName ( String hostName ) <ul><li>ทำการแสดง IP Address ของ Microsoft.com </li></ul><ul><li>import java . net .* ; </li></ul><ul><li>public class AllAddressesOfMicrosoft { </li></ul><ul><li>public static void main ( String [] args ) { </li></ul><ul><li>try { </li></ul><ul><li>InetAddress [] addresses = </li></ul><ul><li>InetAddress . getAllByName (&quot; www . microsoft . com &quot;) ; </li></ul><ul><li>for ( int i = 0; i < addresses . length; i ++) { </li></ul><ul><li>System . out . println ( addresses [ i ]) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch ( UnknownHostException ex ) { </li></ul><ul><li>System . out . println (&quot; Could not find www . microsoft . com &quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  9. 9. InetAddress [ ] getAllByName ( String hostName ) <ul><li>ผลลัพธ์ </li></ul><ul><li>www . microsoft . com / 63.211.66.123 </li></ul><ul><li>www . microsoft . com / 63.211.66.124 </li></ul><ul><li>www . microsoft . com / 63.211.66.131 </li></ul><ul><li>www . microsoft . com / 63.211.66.117 </li></ul><ul><li>www . microsoft . com / 63.211.66.116 </li></ul><ul><li>www . microsoft . com / 63.211.66.107 </li></ul><ul><li>www . microsoft . com / 63.211.66.118 </li></ul><ul><li>www . microsoft . com / 63.211.66.115 </li></ul><ul><li>www . microsoft . com / 63.211.66.110 </li></ul>
  10. 10. public byte[] getAddress( ) <ul><li>การหาค่า IP Address โดยมาเก็บใน Byte Array </li></ul><ul><li>โดยที่ ต้อง แปลงค่า ก่อนนำมาแสดงผลโดยที่ ถ้าค่าที่ได้ติดลบให้บวกด้วย 256 </li></ul><ul><li>InetAddress address = InetAddress . getByName (&quot; www . fareastern . ac . th &quot;) ; </li></ul><ul><li>byte [] ips = address . getAddress () ; </li></ul>
  11. 11. public byte[] getAddress( ) <ul><li>import java . net .* ; </li></ul><ul><li>public class showHostNameByByteArray{ </li></ul><ul><li>public static void main ( String [] args ) { </li></ul><ul><li>try { </li></ul><ul><li>InetAddress address = InetAddress . getByName (&quot; www . fareastern . ac . th &quot;) ; </li></ul><ul><li>byte [] ips = address . getAddress () ; </li></ul><ul><li>for ( int i = 0;i<ips . length;i ++) { </li></ul><ul><li> int unsignedByte = ips [ i ] < 0 ? ips [ i ] + 256 : ips [ i ] ; </li></ul><ul><li>System . out . print ( unsignedByte +&quot;.&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch ( UnknownHostException ex ) { </li></ul><ul><li>System . out . println (&quot; Could not find &quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>ผลลัพธ์ 203.146.43.135 </li></ul>
  12. 12. InetAddress getLocalHost ( ) <ul><li>Return InetAddress ของเครื่องที่ใช้อยู่ </li></ul><ul><li>InetAddress me = InetAddress . getLocalHost ( ) ; </li></ul>import java . net .* ; public class showipLocal { public static void main ( String [] args ) { try { InetAddress address = InetAddress . getLocalHost ( ) ; System . out . println ( address ) ; } catch ( UnknownHostException ex ) { System . out . println (&quot; Could not find this computer's address .&quot;) ; } } }
  13. 13. Testing Reachability <ul><li>ทำการตรวจสอบ การเชื่อมต่อกับ Host ปลายทางว่าสามารถที่จะเชื่อมต่อได้หรือไม่ </li></ul><ul><li>ใช้ได้กับ java 1.5 ขึ้นไป </li></ul><ul><li>ถ้าเกิดข้อผิดพลาดจะ thows IOException </li></ul><ul><li>อาจจะไม่สามารถเชื่อมต่อได้ เนื่องจากติด Firewall </li></ul><ul><li>public boolean isReachable ( int timeout ) </li></ul><ul><li>public boolean isReachable ( NetworkInterface interface, int ttl, int timeout ) </li></ul><ul><li>โดยที่ NetworkInterface คือ Network card ของเครื่อง </li></ul><ul><li>ttl(time-to-live) คือจำนวนสูงสุดของ Network Hops ที่ใช้ </li></ul>
  14. 14. Testing Reachability <ul><li>import java . net .* ; </li></ul><ul><li>import java . util .* ; </li></ul><ul><li>import javax . swing .* ; </li></ul><ul><li>public class testIsReach { </li></ul><ul><li>public static void main ( String [] args ) { </li></ul><ul><li>try { </li></ul><ul><li> InetAddress address = InetAddress . getByName ( “192.168.0.2” ) ; </li></ul><ul><li>long start = new Date (). getTime () ; </li></ul><ul><li>if ( address . isReachable ( 3000 )) { </li></ul><ul><li>long end = new Date (). getTime ()- start; </li></ul><ul><li> System . out . println ( address +&quot; &quot;+ end ) ; </li></ul><ul><li>}else </li></ul><ul><li> System . out . println (&quot; Error &quot;) ; </li></ul><ul><li>} </li></ul><ul><li>catch ( Exception ex ) { </li></ul><ul><li>System . out . println (&quot; Could not find &quot;+ doMainName ) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>// ผลลัพธ์ /192.168.0.2 1026 </li></ul>
  15. 15. Address Types <ul><li>public boolean isAnyLocalAddress ( ) public boolean isLoopbackAddress ( ) public boolean isLinkLocalAddress ( ) public boolean isSiteLocalAddress ( ) public boolean isMulticastAddress ( ) public boolean isMCGlobal ( ) public boolean isMCNodeLocal ( ) public boolean isMCLinkLocal ( ) public boolean isMCSiteLocal ( ) public boolean isMCOrgLocal ( ) </li></ul>
  16. 16. Address Types <ul><li>import java . net .* ; </li></ul><ul><li>import javax . swing .* ; </li></ul><ul><li>public class IPCharacteristics { </li></ul><ul><li>public static void main ( String [] args ) { </li></ul><ul><li>String ip =&quot;&quot; ; </li></ul><ul><li>try { </li></ul><ul><li> ip = JOptionPane . showInputDialog (&quot; Input IP Address :&quot;) ; </li></ul><ul><li>InetAddress address = InetAddress . getByName ( ip ) ; </li></ul><ul><li>if ( address . isAnyLocalAddress ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is a wildcard address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>if ( address . isLoopbackAddress ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is loopback address .&quot;) ; </li></ul><ul><li>} </li></ul>
  17. 17. Address Types <ul><li>if ( address . isLinkLocalAddress ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is a link - local address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else if ( address . isSiteLocalAddress ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is a site - local address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>System . out . println ( address + &quot; is a global address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>if ( address . isMulticastAddress ( )) { </li></ul><ul><li>if ( address . isMCGlobal ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is a global multicast address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else if ( address . isMCOrgLocal ( )) { </li></ul><ul><li>System . out . println ( address </li></ul><ul><li>+ &quot; is an organization wide multicast address .&quot;) ; </li></ul><ul><li>} </li></ul>
  18. 18. Address Types <ul><li>else if ( address . isMCSiteLocal ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is a site wide multicast address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else if ( address . isMCLinkLocal ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is a subnet wide multicast address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else if ( address . isMCNodeLocal ( )) { </li></ul><ul><li>System . out . println ( address + &quot; is an interface - local multicast address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>System . out . println ( address + &quot; is an unknown multicast address type .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>System . out . println ( address + &quot; is a unicast address .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch ( UnknownHostException ex ) { </li></ul><ul><li>System . err . println (&quot; Could not resolve &quot; + args [ 0 ]) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. public boolean equals ( Object o ) <ul><li>ตรวจสอบ ชื่อ Host Name ว่าอ้างถึง IP เดียวกันหรือไม่ </li></ul><ul><li>import java . net .* ; </li></ul><ul><li>public class CheckSameIP { </li></ul><ul><li>public static void main ( String args []) { </li></ul><ul><li>try { </li></ul><ul><li>InetAddress ibiblio = InetAddress . getByName (&quot; www . ibiblio . org &quot;) ; </li></ul><ul><li>InetAddress helios = InetAddress . getByName (&quot; helios . metalab . unc . edu &quot;) ; </li></ul><ul><li>if ( ibiblio . equals ( helios )) { </li></ul><ul><li>System . out . println </li></ul><ul><li>(&quot; www . ibiblio . org is the same as helios . metalab . unc . edu &quot;) ; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>System . out . println </li></ul><ul><li>(&quot; www . ibiblio . org is not the same as helios . metalab . unc . edu &quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch ( UnknownHostException ex ) { </li></ul><ul><li>System . out . println (&quot; Host lookup failed .&quot;) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  20. 20. The NetworkInterface Class <ul><li>ทำหน้าที่ ตรวจสอบ Network Card ภายในเครื่อง </li></ul><ul><li>แสดง Network Card ภายในเครื่อง </li></ul><ul><li>ถ้า error จะ thows SocketException </li></ul><ul><li>ค้นหา primary Ethernet interface </li></ul>try { NetworkInterface ni = NetworkInterface . getByName (&quot; eth0 &quot;) ; if ( ni == null ) { System . err . println (&quot; No such interface : eth0 &quot; ) ; } } catch ( SocketException ex ) { System . err . println (&quot; Could not list sockets .&quot; ) ; }
  21. 21. The NetworkInterface Class <ul><li>โปรแกรมค้นหา NetworkInterface ภายในเครื่อง </li></ul><ul><li>import java . net .* ; </li></ul><ul><li>import java . util .* ; </li></ul><ul><li>public class findNetworkInterface { </li></ul><ul><li>public static void main ( String [] args ) throws Exception { </li></ul><ul><li>Enumeration interfaces = NetworkInterface . getNetworkInterfaces ( ) ; </li></ul><ul><li>while ( interfaces . hasMoreElements ( )) { </li></ul><ul><li>NetworkInterface ni = ( NetworkInterface ) interfaces . nextElement ( ) ; </li></ul><ul><li>System . out . println ( ni ) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  22. 22. The NetworkInterface Class <ul><li>ผลลัพธ์ ( ขึ้นอยู่กับเครื่อง ) </li></ul><ul><li>name : lo ( MS TCP Loopback interface ) index : 1 addresses :/ 127.0.0.1; </li></ul><ul><li>name : eth0 ( NVIDIA nForce Networking Controller - Packet Scheduler Miniport ) index : 2 addresses : </li></ul><ul><li>name : eth1 ( Broadcom 802.11b / g WLAN - Packet Scheduler Miniport ) index : 131075 addresses : </li></ul><ul><li>/ 192.168.10.108 </li></ul>
  23. 23. public Enumeration getInetAddresses ( ) <ul><li>ใน 1 Network interface อาจจะมีหลาย IP </li></ul><ul><li>ใช้แสดง IP Address ของ Network interface </li></ul>NetworkInterface eth0 = NetworkInterrface . getByName (&quot; eth0 &quot;) ; Enumeration addresses = eth0 . getInetAddresses ( ) ; while ( addresses . hasMoreElements ( )) { System . out . println ( addresses . nextElement ( )) ; }

×