KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > net > JInetAddress


1 /*
2     =========================================================================
3     Package net - Implements Network classes.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JInetAddress.java,v 1.4 2007/01/28 17:39:21 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.4 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.net;
40
41
42
43 public class JInetAddress {
44
45   static java.lang.String JavaDoc strAddr1 = "10.";
46   static java.lang.String JavaDoc strAddr2 = "172.16";
47   static java.lang.String JavaDoc strAddr3 = "172.31";
48   static java.lang.String JavaDoc strAddr4 = "192.168";
49   static java.lang.String JavaDoc strAddr5 = "127";
50   
51   
52   
53   public JInetAddress() {
54   /**
55    * Create a internetaddress that encapsulates
56    * specific internet rules for Internet addresses.
57    */

58     
59   }
60   
61   static public java.net.InetAddress JavaDoc getInetAddress() {
62   /**
63    * Returns a valid internet address.
64    * This function searches for a valid
65    * internet IP address through all
66    * adapaters attached in machine.
67    * If none adapter is connected to
68    * internet with valid IP the function
69    * returns null.
70    */

71
72     try {
73       java.net.InetAddress JavaDoc[] addresses = java.net.InetAddress.getAllByName( java.net.InetAddress.getLocalHost().getHostName() );
74       java.lang.String JavaDoc strHostAddress;
75      
76     
77       
78       for( int nCountAdapters = 0; nCountAdapters < addresses.length; nCountAdapters++ ) {
79         strHostAddress = addresses[nCountAdapters].getHostAddress();
80      
81         // Checks for 10 IP Address
82
if( strHostAddress.substring( 0, strAddr1.length() ).compareTo( strAddr1 ) == 0 )
83           continue;
84       
85         // Checks for 172.16 through 172.31 IP Address
86
if( ( strHostAddress.substring( 0, strAddr2.length() ).compareTo( strAddr2 ) == 0 ) || ( strHostAddress.substring( 0, strAddr3.length() ).compareTo( strAddr3 ) == 0 ) )
87           continue;
88       
89         // Checks for 192.168 IP Address
90
if( strHostAddress.substring( 0, strAddr4.length() ).compareTo( strAddr4 ) == 0 )
91           continue;
92         
93         // Checks for 127 IP address
94
if( strHostAddress.substring( 0, strAddr5.length() ).compareTo( strAddr5 ) == 0 )
95           continue;
96         
97         return addresses[nCountAdapters];
98       }
99     } catch( java.net.UnknownHostException JavaDoc e ) {
100       System.err.println( e );
101     }
102    
103     return null;
104   }
105 }
106
107 // InetAddress Class
Popular Tags