java.net
Class InetSocketAddress

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.net.SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress 
      extended by java.net.InetSocketAddress
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable

public class InetSocketAddress
extends SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress

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.

Since:
1.4
See Also:
Socket sample code for java.net.Socket definition code for java.net.Socket , ServerSocket sample code for java.net.ServerSocket definition code for java.net.ServerSocket , Serialized Form

Constructor Summary
InetSocketAddress sample code for java.net.InetSocketAddress.InetSocketAddress(java.net.InetAddress, int) definition code for java.net.InetSocketAddress.InetSocketAddress(java.net.InetAddress, int) (InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr, int port)
          Creates a socket address from an IP address and a port number.
InetSocketAddress sample code for java.net.InetSocketAddress.InetSocketAddress(int) definition code for java.net.InetSocketAddress.InetSocketAddress(int) (int port)
          Creates a socket address where the IP address is the wildcard address and the port number a specified value.
InetSocketAddress sample code for java.net.InetSocketAddress.InetSocketAddress(java.lang.String, int) definition code for java.net.InetSocketAddress.InetSocketAddress(java.lang.String, int) (String sample code for java.lang.String definition code for java.lang.String  hostname, int port)
          Creates a socket address from a hostname and a port number.
 
Method Summary
static InetSocketAddress sample code for java.net.InetSocketAddress definition code for java.net.InetSocketAddress createUnresolved sample code for java.net.InetSocketAddress.createUnresolved(java.lang.String, int) definition code for java.net.InetSocketAddress.createUnresolved(java.lang.String, int) (String sample code for java.lang.String definition code for java.lang.String  host, int port)
          Creates an unresolved socket address from a hostname and a port number.
 boolean equals sample code for java.net.InetSocketAddress.equals(java.lang.Object) definition code for java.net.InetSocketAddress.equals(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          Compares this object against the specified object.
 InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress getAddress sample code for java.net.InetSocketAddress.getAddress() definition code for java.net.InetSocketAddress.getAddress() ()
          Gets the InetAddress.
 String sample code for java.lang.String definition code for java.lang.String getHostName sample code for java.net.InetSocketAddress.getHostName() definition code for java.net.InetSocketAddress.getHostName() ()
          Gets the hostname.
 int getPort sample code for java.net.InetSocketAddress.getPort() definition code for java.net.InetSocketAddress.getPort() ()
          Gets the port number.
 int hashCode sample code for java.net.InetSocketAddress.hashCode() definition code for java.net.InetSocketAddress.hashCode() ()
          Returns a hashcode for this socket address.
 boolean isUnresolved sample code for java.net.InetSocketAddress.isUnresolved() definition code for java.net.InetSocketAddress.isUnresolved() ()
          Checks whether the address has been resolved or not.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.net.InetSocketAddress.toString() definition code for java.net.InetSocketAddress.toString() ()
          Constructs a string representation of this InetSocketAddress.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

InetSocketAddress sample code for java.net.InetSocketAddress(int) definition code for java.net.InetSocketAddress(int)

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.

Parameters:
port - The port number
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the port parameter is outside the specified range of valid port values.

InetSocketAddress sample code for java.net.InetSocketAddress(java.net.InetAddress, int) definition code for java.net.InetSocketAddress(java.net.InetAddress, int)

public InetSocketAddress(InetAddress sample code for java.net.InetAddress definition code for java.net.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.

Parameters:
addr - The IP address
port - The port number
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the port parameter is outside the specified range of valid port values.

InetSocketAddress sample code for java.net.InetSocketAddress(java.lang.String, int) definition code for java.net.InetSocketAddress(java.lang.String, int)

public InetSocketAddress(String sample code for java.lang.String definition code for java.lang.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.

Parameters:
hostname - the Host name
port - The port number
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the port parameter is outside the range of valid port values, or if the hostname parameter is null.
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager is present and permission to resolve the host name is denied.
See Also:
isUnresolved() sample code for java.net.InetSocketAddress.isUnresolved() definition code for java.net.InetSocketAddress.isUnresolved()
Method Detail

createUnresolved sample code for java.net.InetSocketAddress.createUnresolved(java.lang.String, int) definition code for java.net.InetSocketAddress.createUnresolved(java.lang.String, int)

public static InetSocketAddress sample code for java.net.InetSocketAddress definition code for java.net.InetSocketAddress  createUnresolved(String sample code for java.lang.String definition code for 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.

Parameters:
host - the Host name
port - The port number
Returns:
a InetSocketAddress representing the unresolved socket address
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the port parameter is outside the range of valid port values, or if the hostname parameter is null.
Since:
1.5
See Also:
isUnresolved() sample code for java.net.InetSocketAddress.isUnresolved() definition code for java.net.InetSocketAddress.isUnresolved()

getPort sample code for java.net.InetSocketAddress.getPort() definition code for java.net.InetSocketAddress.getPort()

public final int getPort()
Gets the port number.

Returns:
the port number.

getAddress sample code for java.net.InetSocketAddress.getAddress() definition code for java.net.InetSocketAddress.getAddress()

public final InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  getAddress()
Gets the InetAddress.

Returns:
the InetAdress or null if it is unresolved.

getHostName sample code for java.net.InetSocketAddress.getHostName() definition code for java.net.InetSocketAddress.getHostName()

public final String sample code for java.lang.String definition code for java.lang.String  getHostName()
Gets the hostname.

Returns:
the hostname part of the address.

isUnresolved sample code for java.net.InetSocketAddress.isUnresolved() definition code for java.net.InetSocketAddress.isUnresolved()

public final boolean isUnresolved()
Checks whether the address has been resolved or not.

Returns:
true if the hostname couldn't be resolved into an InetAddress.

toString sample code for java.net.InetSocketAddress.toString() definition code for java.net.InetSocketAddress.toString()

public String sample code for java.lang.String definition code for 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.

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a string representation of this object.

equals sample code for java.net.InetSocketAddress.equals(java.lang.Object) definition code for java.net.InetSocketAddress.equals(java.lang.Object)

public final boolean equals(Object sample code for java.lang.Object definition code for 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.

Overrides:
equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) in class Object sample code for java.lang.Object definition code for java.lang.Object
Parameters:
obj - the object to compare against.
Returns:
true if the objects are the same; false otherwise.
See Also:
InetAddress.equals(java.lang.Object) sample code for java.net.InetAddress.equals(java.lang.Object) definition code for java.net.InetAddress.equals(java.lang.Object)

hashCode sample code for java.net.InetSocketAddress.hashCode() definition code for java.net.InetSocketAddress.hashCode()

public final int hashCode()
Returns a hashcode for this socket address.

Overrides:
hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a hash code value for this socket address.
See Also:
Object.equals(java.lang.Object) sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable