java.net
public class: InetSocketAddress [javadoc |
source]
java.lang.Object
java.net.SocketAddress
java.net.InetSocketAddress
All Implemented Interfaces:
Serializable
This class implements an IP Socket Address (IP address + port number)
It can also be a pair (hostname + port number), in which case an attempt
will be made to resolve the hostname. If resolution fails then the address
is said to be
unresolved but can still be used on some circumstances
like connecting through a proxy.
It provides an immutable object used by sockets for binding, connecting, or
as returned values.
The wildcard is a special local IP address. It usually means "any"
and can only be used for bind
operations.
Constructor: |
public InetSocketAddress(int port) {
this(InetAddress.anyLocalAddress(), 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.
Parameters:
port - The port number
Throws:
IllegalArgumentException - if the port parameter is outside the specified
range of valid port values.
|
public InetSocketAddress(InetAddress addr,
int port) {
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;
}
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.
Parameters:
addr - The IP address
port - The port number
Throws:
IllegalArgumentException - if the port parameter is outside the specified
range of valid port values.
|
public InetSocketAddress(String hostname,
int port) {
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;
}
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.
Parameters:
hostname - the Host name
port - The port number
Throws:
IllegalArgumentException - if the port parameter is outside the range
of valid port values, or if the hostname parameter is null.
SecurityException - if a security manager is present and
permission to resolve the host name is
denied.
Also see:
- isUnresolved()
|
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |