Download The InetAddress Class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
The InetAddress Class
The InetAddress Class
ใช้แสดงและจัดการค้นหา IP Address ,Host Name ของเครื่ อง
คอมพิวเตอร์ ที่ อยูใ่ นระบบ Network
ั IPv4 และ IPv6
 ใช้ได้กบ
 ค้นหาผ่าน Local DNS Server
 การเรี ยกใช้ import java.net.InetAddress

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
InetAddress.getByName( )
 การค้นหา Address
จาก ชื่อเครื่ องคอมพิวเตอร์

java.net.InetAddress address = java.net.InetAddress.getByName("www.oreilly.com");

InetAddress address = InetAddress.getByName("www.oreilly.com");
ตัวอย่าง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);
}
}
}
ตัวอย่างInetAddress.getByName( )
 แสดง ชื่อ Domain








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);
}




Name จาก IP Address
}
}
InetAddress[ ] getAllByName(String hostName)
่ นNetwork
 คอมพิวเตอร์ ที่อยูบ
อาจจะมีหลาย IP Address
 ทาเก็บ IP Address ใน Array

InetAddress[] addresses =
InetAddress.getAllByName("www.apple.com");
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("Could not find www.microsoft.com");
}
}
}
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
public byte[] getAddress( )
 การหาค่า
IP Address โดยมาเก็บใน Byte Array
 โดยที่ ต้อง แปลงค่า ก่อนนามาแสดงผลโดยที่ ถ้าค่าที่ได้ตดิ ลบให้บวก
ด้วย 256


InetAddress address =
InetAddress.getByName("www.fareastern.ac.th");
byte[] ips=address.getAddress();
public byte[] getAddress( )





import java.net.*;
public class showHostNameByByteArray{
public static void main (String[] args) {
try {
InetAddress address = InetAddress.getByName("www.fareastern.ac.th");
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+".");





}
}






catch (UnknownHostException ex) {
System.out.println("Could not find ");
}
}
}
ผลลัพธ์ 203.146.43.135
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("Could not find this computer's address.");
}
}
}
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 ที่ใช้
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 +" "+end);
}else
System.out.println("Error ");
}
catch (Exception ex) {
System.out.println("Could not find "+doMainName);
}
}
}
//ผลลัพธ์ /192.168.0.2 1026
Address Types
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( )
 public
ตัวอย่างการตรวจสอบAddress Types


import java.net.*;
import javax.swing.*;













public class IPCharacteristics {
public static void main(String[] args) {
String ip="";
try {
ip=JOptionPane.showInputDialog("Input IP Address :");
InetAddress address = InetAddress.getByName(ip);
if (address.isAnyLocalAddress( )) {
System.out.println(address + " is a wildcard address.");
}
if (address.isLoopbackAddress( )) {
System.out.println(address + " is loopback address.");
}
ตัวอย่างการตรวจสอบAddress Types


















if (address.isLinkLocalAddress( )) {
System.out.println(address + " is a link-local address.");
}
else if (address.isSiteLocalAddress( )) {
System.out.println(address + " is a site-local address.");
}
else {
System.out.println(address + " is a global address.");
}
if (address.isMulticastAddress( )) {
if (address.isMCGlobal( )) {
System.out.println(address + " is a global multicast address.");
}
else if (address.isMCOrgLocal( )) {
System.out.println(address
+ " is an organization wide multicast address.");
}
ตัวอย่างการตรวจสอบAddress Types






















else if (address.isMCSiteLocal( )) {
System.out.println(address + " is a site wide multicast address.");
}
else if (address.isMCLinkLocal( )) {
System.out.println(address + " is a subnet wide multicast address.");
}
else if (address.isMCNodeLocal( )) {
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 (UnknownHostException ex) {
System.err.println("Could not resolve " + args[0]);
}
}
}
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("www.ibiblio.org");
InetAddress helios = InetAddress.getByName("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 (UnknownHostException ex) {
System.out.println("Host lookup failed.");
}
}
}



















The NetworkInterface Class
ทาหน้าที่ ตรวจสอบ Network Card ภายในเครื่ อง
 แสดง Network Card ภายในเครื่ อง
 ถ้า error จะ thows SocketException
 ค้นหา primary Ethernet interface

try {
NetworkInterface ni = NetworkInterface.getByName("eth0");
if (ni == null) {
System.err.println("No such interface: eth0" );
}
}
catch (SocketException ex) {
System.err.println("Could not list sockets." );
}
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);
}
}
}
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

public Enumeration getInetAddresses( )
 ใน 1
Network interface อาจจะมีหลาย IP
 ใช้แสดง IP Address ของ Network interface
NetworkInterface eth0 = NetworkInterrface.getByName("eth0");
Enumeration addresses = eth0.getInetAddresses( );
while (addresses.hasMoreElements( )) {
System.out.println(addresses.nextElement( ));
}
Related documents