Your SlideShare is downloading. ×
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Inet
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Inet

1,156

Published on

PPT

PPT

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,156
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×