KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > InetSocketAddress


1 /*
2  * @(#)InetSocketAddress.java 1.22 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
9 import java.io.ObjectInputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InvalidObjectException JavaDoc;
12
13 /**
14  *
15  * This class implements an IP Socket Address (IP address + port number)
16  * It can also be a pair (hostname + port number), in which case an attempt
17  * will be made to resolve the hostname. If resolution fails then the address
18  * is said to be <I>unresolved</I> but can still be used on some circumstances
19  * like connecting through a proxy.
20  * <p>
21  * It provides an immutable object used by sockets for binding, connecting, or
22  * as returned values.
23  * <p>
24  * The <i>wildcard</i> is a special local IP address. It usually means "any"
25  * and can only be used for <code>bind</code> operations.
26  *
27  * @see java.net.Socket
28  * @see java.net.ServerSocket
29  * @since 1.4
30  */

31 public class InetSocketAddress extends SocketAddress JavaDoc {
32     /* The hostname of the Socket Address
33      * @serial
34      */

35     private String JavaDoc hostname = null;
36     /* The IP address of the Socket Address
37      * @serial
38      */

39     private InetAddress JavaDoc addr = null;
40     /* The port number of the Socket Address
41      * @serial
42      */

43     private int port;
44
45     private static final long serialVersionUID = 5076001401234631237L;
46     
47     private InetSocketAddress() {
48     }
49
50     /**
51      * Creates a socket address where the IP address is the wildcard address
52      * and the port number a specified value.
53      * <p>
54      * A valid port value is between 0 and 65535.
55      * A port number of <code>zero</code> will let the system pick up an
56      * ephemeral port in a <code>bind</code> operation.
57      * <p>
58      * @param port The port number
59      * @throws IllegalArgumentException if the port parameter is outside the specified
60      * range of valid port values.
61      */

62     public InetSocketAddress(int port) {
63     this(InetAddress.anyLocalAddress(), port);
64     }
65
66     /**
67      *
68      * Creates a socket address from an IP address and a port number.
69      * <p>
70      * A valid port value is between 0 and 65535.
71      * A port number of <code>zero</code> will let the system pick up an
72      * ephemeral port in a <code>bind</code> operation.
73      * <P>
74      * A <code>null</code> address will assign the <i>wildcard</i> address.
75      * <p>
76      * @param addr The IP address
77      * @param port The port number
78      * @throws IllegalArgumentException if the port parameter is outside the specified
79      * range of valid port values.
80      */

81     public InetSocketAddress(InetAddress JavaDoc addr, int port) {
82     if (port < 0 || port > 0xFFFF) {
83         throw new IllegalArgumentException JavaDoc("port out of range:" + port);
84     }
85     this.port = port;
86     if (addr == null)
87         this.addr = InetAddress.anyLocalAddress();
88     else
89         this.addr = addr;
90     }
91
92     /**
93      *
94      * Creates a socket address from a hostname and a port number.
95      * <p>
96      * An attempt will be made to resolve the hostname into an InetAddress.
97      * If that attempt fails, the address will be flagged as <I>unresolved</I>.
98      * <p>
99      * If there is a security manager, its <code>checkConnect</code> method
100      * is called with the host name as its argument to check the permissiom
101      * to resolve it. This could result in a SecurityException.
102      * <P>
103      * A valid port value is between 0 and 65535.
104      * A port number of <code>zero</code> will let the system pick up an
105      * ephemeral port in a <code>bind</code> operation.
106      * <P>
107      * @param hostname the Host name
108      * @param port The port number
109      * @throws IllegalArgumentException if the port parameter is outside the range
110      * of valid port values, or if the hostname parameter is <TT>null</TT>.
111      * @throws SecurityException if a security manager is present and
112      * permission to resolve the host name is
113      * denied.
114      * @see #isUnresolved()
115      */

116     public InetSocketAddress(String JavaDoc hostname, int port) {
117     if (port < 0 || port > 0xFFFF) {
118         throw new IllegalArgumentException JavaDoc("port out of range:" + port);
119     }
120     if (hostname == null) {
121         throw new IllegalArgumentException JavaDoc("hostname can't be null");
122     }
123     try {
124         addr = InetAddress.getByName(hostname);
125     } catch(UnknownHostException JavaDoc e) {
126         this.hostname = hostname;
127         addr = null;
128     }
129     this.port = port;
130     }
131
132     /**
133      *
134      * Creates an unresolved socket address from a hostname and a port number.
135      * <p>
136      * No attempt will be made to resolve the hostname into an InetAddress.
137      * The address will be flagged as <I>unresolved</I>.
138      * <p>
139      * A valid port value is between 0 and 65535.
140      * A port number of <code>zero</code> will let the system pick up an
141      * ephemeral port in a <code>bind</code> operation.
142      * <P>
143      * @param host the Host name
144      * @param port The port number
145      * @throws IllegalArgumentException if the port parameter is outside
146      * the range of valid port values, or if the hostname
147      * parameter is <TT>null</TT>.
148      * @see #isUnresolved()
149      * @return a <code>InetSocketAddress</code> representing the unresolved
150      * socket address
151      * @since 1.5
152      */

153     public static InetSocketAddress JavaDoc createUnresolved(String JavaDoc host, int port) {
154     if (port < 0 || port > 0xFFFF) {
155         throw new IllegalArgumentException JavaDoc("port out of range:" + port);
156     }
157     if (host == null) {
158         throw new IllegalArgumentException JavaDoc("hostname can't be null");
159     }
160     InetSocketAddress JavaDoc s = new InetSocketAddress JavaDoc();
161     s.port = port;
162     s.hostname = host;
163     s.addr = null;
164     return s;
165     }
166
167     private void readObject(ObjectInputStream JavaDoc s)
168     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
169     s.defaultReadObject();
170     
171     // Check that our invariants are satisfied
172
if (port < 0 || port > 0xFFFF) {
173         throw new InvalidObjectException JavaDoc("port out of range:" + port);
174     }
175     
176     if (hostname == null && addr == null) {
177         throw new InvalidObjectException JavaDoc("hostname and addr " +
178                          "can't both be null");
179     }
180     }
181
182     /**
183      * Gets the port number.
184      *
185      * @return the port number.
186      */

187     public final int getPort() {
188     return port;
189     }
190
191     /**
192      *
193      * Gets the <code>InetAddress</code>.
194      *
195      * @return the InetAdress or <code>null</code> if it is unresolved.
196      */

197     public final InetAddress JavaDoc getAddress() {
198     return addr;
199     }
200
201     /**
202      * Gets the <code>hostname</code>.
203      *
204      * @return the hostname part of the address.
205      */

206     public final String JavaDoc getHostName() {
207     if (hostname != null)
208         return hostname;
209     if (addr != null)
210         return addr.getHostName();
211     return null;
212     }
213
214     /**
215      * Returns the hostname, or the String form of the address if it
216      * doesn't have a hostname (it was created using a litteral).
217      * This has the benefit of <b>not</b> attemptimg a reverse lookup.
218      *
219      * @return the hostname, or String representation of the address.
220      * @since 1.6
221      */

222     final String JavaDoc getHostString() {
223     if (hostname != null)
224         return hostname;
225     if (addr != null) {
226         if (addr.hostName != null)
227         return addr.hostName;
228         else
229         return addr.getHostAddress();
230     }
231     return null;
232     }
233
234     /**
235      * Checks whether the address has been resolved or not.
236      *
237      * @return <code>true</code> if the hostname couldn't be resolved into
238      * an <code>InetAddress</code>.
239      */

240     public final boolean isUnresolved() {
241     return addr == null;
242     }
243
244     /**
245      * Constructs a string representation of this InetSocketAddress.
246      * This String is constructed by calling toString() on the InetAddress
247      * and concatenating the port number (with a colon). If the address
248      * is unresolved then the part before the colon will only contain the hostname.
249      *
250      * @return a string representation of this object.
251      */

252     public String JavaDoc toString() {
253     if (isUnresolved()) {
254         return hostname + ":" + port;
255     } else {
256         return addr.toString() + ":" + port;
257     }
258     }
259
260     /**
261      * Compares this object against the specified object.
262      * The result is <code>true</code> if and only if the argument is
263      * not <code>null</code> and it represents the same address as
264      * this object.
265      * <p>
266      * Two instances of <code>InetSocketAddress</code> represent the same
267      * address if both the InetAddresses (or hostnames if it is unresolved) and port
268      * numbers are equal.
269      * If both addresses are unresolved, then the hostname & the port number
270      * are compared.
271      *
272      * @param obj the object to compare against.
273      * @return <code>true</code> if the objects are the same;
274      * <code>false</code> otherwise.
275      * @see java.net.InetAddress#equals(java.lang.Object)
276      */

277     public final boolean equals(Object JavaDoc obj) {
278     if (obj == null || !(obj instanceof InetSocketAddress JavaDoc))
279         return false;
280     InetSocketAddress JavaDoc sockAddr = (InetSocketAddress JavaDoc) obj;
281     boolean sameIP = false;
282     if (this.addr != null)
283         sameIP = this.addr.equals(sockAddr.addr);
284     else if (this.hostname != null)
285         sameIP = (sockAddr.addr == null) &&
286         this.hostname.equals(sockAddr.hostname);
287     else
288         sameIP = (sockAddr.addr == null) && (sockAddr.hostname == null);
289     return sameIP && (this.port == sockAddr.port);
290     }
291
292     /**
293      * Returns a hashcode for this socket address.
294      *
295      * @return a hash code value for this socket address.
296      */

297     public final int hashCode() {
298     if (addr != null)
299         return addr.hashCode() + port;
300     if (hostname != null)
301         return hostname.hashCode() + port;
302     return port;
303     }
304 }
305
Popular Tags