FileDocCategorySizeDatePackage
InetSocketAddress.javaAPI DocJava SE 5 API9100Fri Aug 26 14:57:08 BST 2005java.net

InetSocketAddress

public class InetSocketAddress 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.

see
java.net.Socket
see
java.net.ServerSocket
since
1.4

Fields Summary
private String
hostname
private InetAddress
addr
private int
port
private static final long
serialVersionUID
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.

param
port The port number
throws
IllegalArgumentException if the port parameter is outside the specified range of valid port values.

	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.

param
addr The IP address
param
port The port number
throws
IllegalArgumentException if the port parameter is outside the specified range of valid port values.

	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.

param
hostname the Host name
param
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.
throws
SecurityException if a security manager is present and permission to resolve the host name is denied.
see
#isUnresolved()

	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.InetSocketAddresscreateUnresolved(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.

param
host the Host name
param
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.
see
#isUnresolved()
return
a InetSocketAddress representing the unresolved socket address
since
1.5

	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 booleanequals(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.

param
obj the object to compare against.
return
true if the objects are the same; false otherwise.
see
java.net.InetAddress#equals(java.lang.Object)

	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.InetAddressgetAddress()
Gets the InetAddress.

return
the InetAdress or null if it is unresolved.

	return addr;
    
public final java.lang.StringgetHostName()
Gets the hostname.

return
the hostname part of the address.

	if (hostname != null)
	    return hostname;
	if (addr != null)
	    return addr.getHostName();
	return null;
    
public final intgetPort()
Gets the port number.

return
the port number.

	return port;
    
public final inthashCode()
Returns a hashcode for this socket address.

return
a hash code value for this socket address.

	if (addr != null)
	    return addr.hashCode() + port;
	if (hostname != null)
	    return hostname.hashCode() + port;
	return port;
    
public final booleanisUnresolved()
Checks whether the address has been resolved or not.

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

	return addr == null;
    
private voidreadObject(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.StringtoString()
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.

return
a string representation of this object.

	if (isUnresolved()) {
	    return hostname + ":" + port;
	} else {
	    return addr.toString() + ":" + port;
	}