public class

InetSocketAddress

extends SocketAddress
java.lang.Object
   ↳ java.net.SocketAddress
     ↳ java.net.InetSocketAddress

Class Overview

This class represents a socket endpoint described by a IP address and a port number. It is a concrete implementation of SocketAddress for IP.

Summary

Public Constructors
InetSocketAddress(int port)
Creates a socket endpoint with the given port number port and no specified address.
InetSocketAddress(InetAddress address, int port)
Creates a socket endpoint with the given port number port and address.
InetSocketAddress(String host, int port)
Creates a socket endpoint with the given port number port and the hostname host.
Public Methods
static InetSocketAddress createUnresolved(String host, int port)
Creates an InetSocketAddress without trying to resolve the hostname into an InetAddress.
final boolean equals(Object socketAddr)
Compares two socket endpoints and returns true if they are equal.
final InetAddress getAddress()
Gets the address of this socket.
final String getHostName()
Returns the hostname, doing a reverse lookup on the InetAddress if no hostname string was provided at construction time.
final int getPort()
Gets the port number of this socket.
final int hashCode()
Returns an integer hash code for this object.
final boolean isUnresolved()
Returns whether this socket address is unresolved or not.
String toString()
Gets a string representation of this socket included the address and the port number.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public InetSocketAddress (int port)

Since: API Level 1

Creates a socket endpoint with the given port number port and no specified address. The range for valid port numbers is between 0 and 65535 inclusive.

Parameters
port the specified port number to which this socket is bound.

public InetSocketAddress (InetAddress address, int port)

Since: API Level 1

Creates a socket endpoint with the given port number port and address. The range for valid port numbers is between 0 and 65535 inclusive. If address is null this socket is bound to the IPv4 wildcard address.

Parameters
address the specified address to which this socket is bound.
port the specified port number to which this socket is bound.

public InetSocketAddress (String host, int port)

Since: API Level 1

Creates a socket endpoint with the given port number port and the hostname host. The hostname is tried to be resolved and cannot be null. The range for valid port numbers is between 0 and 65535 inclusive.

Parameters
host the specified hostname to which this socket is bound.
port the specified port number to which this socket is bound.

Public Methods

public static InetSocketAddress createUnresolved (String host, int port)

Since: API Level 1

Creates an InetSocketAddress without trying to resolve the hostname into an InetAddress. The address field is marked as unresolved.

Parameters
host the specified hostname to which this socket is bound.
port the specified port number to which this socket is bound.
Returns
  • the created InetSocketAddress instance.
Throws
IllegalArgumentException if the hostname host is null or the port is not in the range between 0 and 65535.

public final boolean equals (Object socketAddr)

Since: API Level 1

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.

Parameters
socketAddr the object to be tested for equality.
Returns
  • true if this socket and the given socket object socketAddr are equal, false otherwise.

public final InetAddress getAddress ()

Since: API Level 1

Gets the address of this socket.

Returns
  • the socket endpoint address.

public final String getHostName ()

Since: API Level 1

Returns the hostname, doing a reverse lookup on the InetAddress if no hostname string was provided at construction time.

public final int getPort ()

Since: API Level 1

Gets the port number of this socket.

Returns
  • the socket endpoint port number.

public final int hashCode ()

Since: API Level 1

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.

public final boolean isUnresolved ()

Since: API Level 1

Returns whether this socket address is unresolved or not.

Returns
  • true if this socket address is unresolved, false otherwise.

public String toString ()

Since: API Level 1

Gets a string representation of this socket included the address and the port number.

Returns
  • the address and port number as a textual representation.