FileDocCategorySizeDatePackage
InetSocketAddress.javaAPI DocAndroid 1.5 API8343Wed May 06 22:41:04 BST 2009java.net

InetSocketAddress

public class InetSocketAddress extends SocketAddress
This class represents a socket endpoint described by a IP address and a port number. It is a concrete implementation of {@code SocketAddress} for IP.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private String
hostname
private InetAddress
addr
private int
port
Constructors Summary
public InetSocketAddress(int port)
Creates a socket endpoint with the given port number {@code port} and the wildcard address {@code InetAddress.ANY}. The range for valid port numbers is between 0 and 65535 inclusive.

param
port the specified port number to which this socket is bound.
since
Android 1.0


                                                                 
       
        this((InetAddress) null, port);
    
public InetSocketAddress(InetAddress address, int port)
Creates a socket endpoint with the given port number {@code port} and {@code address}. The range for valid port numbers is between 0 and 65535 inclusive. If {@code address} is {@code null} this socket is bound to the wildcard address {@code InetAddress.ANY}.

param
port the specified port number to which this socket is bound.
param
address the specified address to which this socket is bound.
since
Android 1.0

        if (port < 0 || port > 65535) {
            throw new IllegalArgumentException();
        }
        if (address == null) {
            addr = InetAddress.ANY;
        } else {
            addr = address;
        }
        hostname = addr.getHostName();
        this.port = port;
    
public InetSocketAddress(String host, int port)
Creates a socket endpoint with the given port number {@code port} and the hostname {@code host}. The hostname is tried to be resolved and cannot be {@code null}. The range for valid port numbers is between 0 and 65535 inclusive.

param
port the specified port number to which this socket is bound.
param
host the specified hostname to which this socket is bound.
throws
SecurityException if a {@link SecurityManager} is installed and its {@code checkConnect()} method does not allow the resolving of the host name.
since
Android 1.0

        this(host, port, true);
    
InetSocketAddress(String host, int port, boolean needResolved)

        if (host == null || port < 0 || port > 65535) {
            throw new IllegalArgumentException();
        }
        hostname = host;
        this.port = port;
        if (needResolved) {
            // BEGIN android-added
            SecurityManager smgr = System.getSecurityManager();
            if(smgr != null) {
                smgr.checkConnect(host, port);
            }
            // END android-added
            try {
                addr = InetAddress.getByName(hostname);
                hostname = null;
            } catch (UnknownHostException e) {
                // Ignored
            }
        } else {
            addr = null;
        }
    
Methods Summary
public static java.net.InetSocketAddresscreateUnresolved(java.lang.String host, int port)
Creates an {@code InetSocketAddress} without trying to resolve the hostname into an {@code InetAddress}. The address field is marked as unresolved.

param
host the specified hostname to which this socket is bound.
param
port the specified port number to which this socket is bound.
return
the created InetSocketAddress instance.
throws
IllegalArgumentException if the hostname {@code host} is {@code null} or the port is not in the range between 0 and 65535.
since
Android 1.0

        return new InetSocketAddress(host, port, false);
    
public final booleanequals(java.lang.Object socketAddr)
Compares two socket endpoints and returns true if they are equal. Two socket endpoints are equal if the IP address or the hostname of both are equal and they are bound to the same port.

param
socketAddr the object to be tested for equality.
return
{@code true} if this socket and the given socket object {@code socketAddr} are equal, {@code false} otherwise.
since
Android 1.0

        if (this == socketAddr) {
            return true;
        }
        if (!(socketAddr instanceof InetSocketAddress)) {
            return false;
        }
        InetSocketAddress iSockAddr = (InetSocketAddress) socketAddr;

        // check the ports as we always need to do this
        if (port != iSockAddr.port) {
            return false;
        }

        // we only use the hostnames in the comparison if the addrs were not
        // resolved
        if ((addr == null) && (iSockAddr.addr == null)) {
            return hostname.equals(iSockAddr.hostname);
        }

        // addrs were resolved so use them for the comparison
        if (addr == null) {
            // if we are here we know iSockAddr is not null so just return
            // false
            return false;
        }
        return addr.equals(iSockAddr.addr);
    
public final java.net.InetAddressgetAddress()
Gets the address of this socket.

return
the socket endpoint address.
since
Android 1.0

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

return
the socket endpoint hostname.
since
Android 1.0

        return (null != addr) ? addr.getHostName() : hostname;
    
public final intgetPort()
Gets the port number of this socket.

return
the socket endpoint port number.
since
Android 1.0

        return port;
    
public final inthashCode()
Gets the hashcode of this socket.

return
the appropriate hashcode.
since
Android 1.0

        if (addr == null) {
            return hostname.hashCode() + port;
        }
        return addr.hashCode() + port;
    
public final booleanisUnresolved()
Returns whether this socket address is unresolved or not.

return
{@code true} if this socket address is unresolved, {@code false} otherwise.
since
Android 1.0

        return addr == null;
    
private voidreadObject(java.io.ObjectInputStream stream)

        stream.defaultReadObject();
    
public java.lang.StringtoString()
Gets a string representation of this socket included the address and the port number.

return
the address and port number as a textual representation.
since
Android 1.0

        String host;
        if (addr != null) {
            host = addr.toString();
        } else {
            host = hostname;
        }
        return host + ":" + port; //$NON-NLS-1$