]>
gcc.gnu.org Git - gcc.git/blob - libjava/java/net/InetSocketAddress.java
1 /* InetSocketAddress.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 * InetSocketAddress instances represent socket addresses
42 * in the java.nio package. They encapsulate a InetAddress and
48 public class InetSocketAddress
extends SocketAddress
51 * Compatible with JDK 1.4+
53 private static final long serialVersionUID
= 5076001401234631237L;
60 * Constructs an InetSocketAddress instance.
62 * @param addr Address of the socket
63 * @param port Port if the socket
65 * @exception IllegalArgumentException If the port number is illegel
67 public InetSocketAddress(InetAddress addr
, int port
)
68 throws IllegalArgumentException
70 if (port
< 0 || port
> 65535)
71 throw new IllegalArgumentException();
75 this.hostname
= addr
.getHostName ();
79 * Constructs an InetSocketAddress instance.
81 * @param port Port if the socket
83 * @exception IllegalArgumentException If the port number is illegal
85 public InetSocketAddress(int port
)
86 throws IllegalArgumentException
88 if (port
< 0 || port
> 65535)
89 throw new IllegalArgumentException();
95 byte[] any
= { 0, 0, 0, 0 };
96 this.addr
= InetAddress
.getByAddress (any
);
97 this.hostname
= "0.0.0.0";
99 catch (UnknownHostException e
)
108 * Constructs an InetSocketAddress instance.
110 * @param addr Address of the socket
111 * @param port Port if the socket
113 * @exception IllegalArgumentException If the port number is illegal
115 public InetSocketAddress(String hostname
, int port
)
116 throws IllegalArgumentException
118 if (port
< 0 || port
> 65535)
119 throw new IllegalArgumentException();
122 this.hostname
= hostname
;
126 this.addr
= InetAddress
.getByName(hostname
);
128 catch (Exception e
) // UnknownHostException, SecurityException
135 * Test if obj is a <code>InetSocketAddress</code> and
136 * has the same address and port
138 public final boolean equals (Object obj
)
140 // InetSocketAddress objects are equal when addr and port are equal.
141 // The hostname may differ.
143 if (obj
instanceof InetSocketAddress
)
145 InetSocketAddress a
= (InetSocketAddress
) obj
;
146 return addr
.equals(a
.addr
) && a
.port
== port
;
153 * Returns the <code>InetAddress</code> or
154 * <code>null</code> if its unresolved
156 public final InetAddress
getAddress()
162 * Returns <code>hostname</code>
164 public final String
getHostName()
170 * Returns the <code>port</code>
172 public final int getPort()
178 * Returns the hashcode of the <code>InetSocketAddress</code>
180 public final int hashCode()
182 return port
+ addr
.hashCode();
186 * Checks wether the address has been resolved or not
188 public final boolean isUnresolved()
194 * Returns the <code>InetSocketAddress</code> as string
196 public String
toString()
198 return addr
+ ":" + port
;
This page took 0.047536 seconds and 5 git commands to generate.