KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > Inet4AddressImpl


1 /*
2  * @(#)Inet4AddressImpl.java 1.6 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.net;
8 import java.io.IOException JavaDoc;
9
10 /*
11  * Package private implementation of InetAddressImpl for IPv4.
12  *
13  * @since 1.4
14  */

15 class Inet4AddressImpl implements InetAddressImpl JavaDoc {
16     public native String JavaDoc getLocalHostName() throws UnknownHostException JavaDoc;
17     public native InetAddress JavaDoc[]
18         lookupAllHostAddr(String JavaDoc hostname) throws UnknownHostException JavaDoc;
19     public native String JavaDoc getHostByAddr(byte[] addr) throws UnknownHostException JavaDoc;
20     private native boolean isReachable0(byte[] addr, int timeout, byte[] ifaddr, int ttl) throws IOException JavaDoc;
21
22     public synchronized InetAddress JavaDoc anyLocalAddress() {
23         if (anyLocalAddress == null) {
24             anyLocalAddress = new Inet4Address JavaDoc(); // {0x00,0x00,0x00,0x00}
25
anyLocalAddress.hostName = "0.0.0.0";
26         }
27         return anyLocalAddress;
28     }
29
30     public synchronized InetAddress JavaDoc loopbackAddress() {
31         if (loopbackAddress == null) {
32             byte[] loopback = {0x7f,0x00,0x00,0x01};
33             loopbackAddress = new Inet4Address JavaDoc("localhost", loopback);
34         }
35         return loopbackAddress;
36     }
37
38   public boolean isReachable(InetAddress JavaDoc addr, int timeout, NetworkInterface JavaDoc netif, int ttl) throws IOException JavaDoc {
39       byte[] ifaddr = null;
40       if (netif != null) {
41       /*
42        * Let's make sure we use an address of the proper family
43        */

44       java.util.Enumeration JavaDoc it = netif.getInetAddresses();
45       InetAddress JavaDoc inetaddr = null;
46       while (!(inetaddr instanceof Inet4Address JavaDoc) &&
47          it.hasMoreElements())
48           inetaddr = (InetAddress JavaDoc) it.nextElement();
49       if (inetaddr instanceof Inet4Address JavaDoc)
50           ifaddr = inetaddr.getAddress();
51       }
52       return isReachable0(addr.getAddress(), timeout, ifaddr, ttl);
53   }
54     private InetAddress JavaDoc anyLocalAddress;
55     private InetAddress JavaDoc loopbackAddress;
56 }
57
58
Popular Tags