public class java.net
InetSocketAddress


Hide details Login
Java SE 6
  
Extends: 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.
since1.4
See also java.net.Socket, java.net.ServerSocket


Constructors
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.

portThe port number
ThrowsIllegalArgumentException: if the port parameter is outside the specified range of valid port values.

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.

addrThe IP address
portThe port number
ThrowsIllegalArgumentException: if the port parameter is outside the specified range of valid port values.

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.

hostnamethe Host name
portThe port number
ThrowsIllegalArgumentException: if the port parameter is outside the range of valid port values, or if the hostname parameter is null.
ThrowsSecurityException: if a security manager is present and permission to resolve the host name is denied.
See also isUnresolved()


Methods
public static InetSocketAddress createUnresolved(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.

hostthe Host name
portThe port number
ThrowsIllegalArgumentException: if the port parameter is outside the range of valid port values, or if the hostname parameter is null.
returna InetSocketAddress representing the unresolved socket address
since1.5
See also isUnresolved()

final public boolean equals(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.
objthe object to compare against.
returntrue if the objects are the same; false otherwise.
See also equals(java.lang.Object)

final public int hashCode()
Returns a hashcode for this socket address.
returna hash code value for this socket address.
public 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.
returna string representation of this object.

Properties
final public InetAddress getAddress()
Gets the InetAddress.
returnthe InetAdress or null if it is unresolved.
final public String getHostName()
Gets the hostname.
returnthe hostname part of the address.
final public int getPort()
Gets the port number.
returnthe port number.
final public boolean isUnresolved()
Checks whether the address has been resolved or not.
returntrue if the hostname couldn't be resolved into an InetAddress.