Package java.net

Class InetSocketAddress

java.lang.Object
java.net.SocketAddress
java.net.InetSocketAddress
All Implemented Interfaces:
Serializable

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 SocketAddress for IP.
See Also:
Serialized Form
  • Constructor Summary

    Constructors
    Constructor Description
    InetSocketAddress()  
    InetSocketAddress​(int port)
    Creates a socket endpoint with the given port number port and no specified address.
    InetSocketAddress​(String host, int port)
    Creates a socket endpoint with the given port number port and the hostname host.
    InetSocketAddress​(InetAddress address, int port)
    Creates a socket endpoint with the given port number port and address.
  • Method Summary

    Modifier and Type Method Description
    static InetSocketAddress createUnresolved​(String host, int port)
    Creates an InetSocketAddress without trying to resolve the hostname into an InetAddress.
    boolean equals​(Object socketAddr)
    Compares two socket endpoints and returns true if they are equal.
    InetAddress getAddress()
    Returns this socket address' address.
    String getHostName()
    Returns the hostname, doing a reverse DNS lookup on the InetAddress if no hostname string was provided at construction time.
    String getHostString()
    Returns the hostname if known, or the result of InetAddress.getHostAddress.
    int getPort()
    Returns this socket address' port.
    int hashCode()
    Returns an integer hash code for this object.
    boolean isUnresolved()
    Returns whether this socket address is unresolved or not.
    String toString()
    Returns a string containing the address (or the hostname for an unresolved InetSocketAddress) and port number.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • InetSocketAddress

      public InetSocketAddress()
    • InetSocketAddress

      public InetSocketAddress​(int port)
      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.
    • InetSocketAddress

      public InetSocketAddress​(InetAddress address, int port)
      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:
      port - the specified port number to which this socket is bound.
      address - the specified address to which this socket is bound.
    • InetSocketAddress

      public InetSocketAddress​(String host, int port)
      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:
      port - the specified port number to which this socket is bound.
      host - the specified hostname to which this socket is bound.
  • Method Details

    • createUnresolved

      public static InetSocketAddress createUnresolved​(String host, int port)
      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.
    • getPort

      public final int getPort()
      Returns this socket address' port.
    • getAddress

      public final InetAddress getAddress()
      Returns this socket address' address.
    • getHostName

      public final String getHostName()
      Returns the hostname, doing a reverse DNS lookup on the InetAddress if no hostname string was provided at construction time. Use getHostString() to avoid the reverse DNS lookup.
    • getHostString

      public final String getHostString()
      Returns the hostname if known, or the result of InetAddress.getHostAddress. Unlike getHostName(), this method will never cause a DNS lookup.
      Since:
      1.7
    • isUnresolved

      public final boolean isUnresolved()
      Returns whether this socket address is unresolved or not.
      Returns:
      true if this socket address is unresolved, false otherwise.
    • toString

      public String toString()
      Returns a string containing the address (or the hostname for an unresolved InetSocketAddress) and port number. For example: "www.google.com/74.125.224.115:80" or "/127.0.0.1:80".
      Overrides:
      toString in class Object
      Returns:
      a printable representation of this object.
    • equals

      public final boolean equals​(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.
      Overrides:
      equals in class Object
      Parameters:
      socketAddr - the object to be tested for equality.
      Returns:
      true if this socket and the given socket object socketAddr are equal, false otherwise.
      See Also:
      Object.hashCode()
    • hashCode

      public final int hashCode()
      Description copied from class: Object
      Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.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.

      Overrides:
      hashCode in class Object
      Returns:
      this object's hash code.
      See Also:
      Object.equals(java.lang.Object)