Constructors Summary |
---|
private InetSocketAddress()
|
public InetSocketAddress(int port)Creates a socket address where the IP address is the wildcard address
and the port number a specified value.
A valid port value is between 0 and 65535.
A port number of zero will let the system pick up an
ephemeral port in a bind operation.
this(InetAddress.anyLocalAddress(), port);
|
public InetSocketAddress(InetAddress addr, int port)Creates a socket address from an IP address and a port number.
A valid port value is between 0 and 65535.
A port number of zero will let the system pick up an
ephemeral port in a bind operation.
A null address will assign the wildcard address.
if (port < 0 || port > 0xFFFF) {
throw new IllegalArgumentException("port out of range:" + port);
}
this.port = port;
if (addr == null)
this.addr = InetAddress.anyLocalAddress();
else
this.addr = addr;
|
public InetSocketAddress(String hostname, int port)Creates a socket address from a hostname and a port number.
An attempt will be made to resolve the hostname into an InetAddress.
If that attempt fails, the address will be flagged as unresolved.
If there is a security manager, its checkConnect method
is called with the host name as its argument to check the permissiom
to resolve it. This could result in a SecurityException.
A valid port value is between 0 and 65535.
A port number of zero will let the system pick up an
ephemeral port in a bind operation.
if (port < 0 || port > 0xFFFF) {
throw new IllegalArgumentException("port out of range:" + port);
}
if (hostname == null) {
throw new IllegalArgumentException("hostname can't be null");
}
try {
addr = InetAddress.getByName(hostname);
} catch(UnknownHostException e) {
this.hostname = hostname;
addr = null;
}
this.port = port;
|
Methods Summary |
---|
public static java.net.InetSocketAddress | createUnresolved(java.lang.String host, int port)Creates an unresolved socket address from a hostname and a port number.
No attempt will be made to resolve the hostname into an InetAddress.
The address will be flagged as unresolved.
A valid port value is between 0 and 65535.
A port number of zero will let the system pick up an
ephemeral port in a bind operation.
if (port < 0 || port > 0xFFFF) {
throw new IllegalArgumentException("port out of range:" + port);
}
if (host == null) {
throw new IllegalArgumentException("hostname can't be null");
}
InetSocketAddress s = new InetSocketAddress();
s.port = port;
s.hostname = host;
s.addr = null;
return s;
|
public final boolean | equals(java.lang.Object obj)Compares this object against the specified object.
The result is true if and only if the argument is
not null and it represents the same address as
this object.
Two instances of InetSocketAddress represent the same
address if both the InetAddresses (or hostnames if it is unresolved) and port
numbers are equal.
If both addresses are unresolved, then the hostname & the port number
are compared.
if (obj == null || !(obj instanceof InetSocketAddress))
return false;
InetSocketAddress sockAddr = (InetSocketAddress) obj;
boolean sameIP = false;
if (this.addr != null)
sameIP = this.addr.equals(sockAddr.addr);
else if (this.hostname != null)
sameIP = (sockAddr.addr == null) &&
this.hostname.equals(sockAddr.hostname);
else
sameIP = (sockAddr.addr == null) && (sockAddr.hostname == null);
return sameIP && (this.port == sockAddr.port);
|
public final java.net.InetAddress | getAddress()Gets the InetAddress .
return addr;
|
public final java.lang.String | getHostName()Gets the hostname .
if (hostname != null)
return hostname;
if (addr != null)
return addr.getHostName();
return null;
|
final java.lang.String | getHostString()Returns the hostname, or the String form of the address if it
doesn't have a hostname (it was created using a litteral).
This has the benefit of not attemptimg a reverse lookup.
if (hostname != null)
return hostname;
if (addr != null) {
if (addr.hostName != null)
return addr.hostName;
else
return addr.getHostAddress();
}
return null;
|
public final int | getPort()Gets the port number.
return port;
|
public final int | hashCode()Returns a hashcode for this socket address.
if (addr != null)
return addr.hashCode() + port;
if (hostname != null)
return hostname.hashCode() + port;
return port;
|
public final boolean | isUnresolved()Checks whether the address has been resolved or not.
return addr == null;
|
private void | readObject(java.io.ObjectInputStream s)
s.defaultReadObject();
// Check that our invariants are satisfied
if (port < 0 || port > 0xFFFF) {
throw new InvalidObjectException("port out of range:" + port);
}
if (hostname == null && addr == null) {
throw new InvalidObjectException("hostname and addr " +
"can't both be null");
}
|
public java.lang.String | toString()Constructs a string representation of this InetSocketAddress.
This String is constructed by calling toString() on the InetAddress
and concatenating the port number (with a colon). If the address
is unresolved then the part before the colon will only contain the hostname.
if (isUnresolved()) {
return hostname + ":" + port;
} else {
return addr.toString() + ":" + port;
}
|