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 numberportand no specified address.InetSocketAddress(String host, int port)Creates a socket endpoint with the given port numberportand the hostnamehost.InetSocketAddress(InetAddress address, int port)Creates a socket endpoint with the given port numberportandaddress. -
Method Summary
Modifier and Type Method Description static InetSocketAddresscreateUnresolved(String host, int port)Creates anInetSocketAddresswithout trying to resolve the hostname into anInetAddress.booleanequals(Object socketAddr)Compares two socket endpoints and returns true if they are equal.InetAddressgetAddress()Returns this socket address' address.StringgetHostName()Returns the hostname, doing a reverse DNS lookup on theInetAddressif no hostname string was provided at construction time.StringgetHostString()Returns the hostname if known, or the result ofInetAddress.getHostAddress.intgetPort()Returns this socket address' port.inthashCode()Returns an integer hash code for this object.booleanisUnresolved()Returns whether this socket address is unresolved or not.StringtoString()Returns a string containing the address (or the hostname for an unresolvedInetSocketAddress) and port number.
-
Constructor Details
-
InetSocketAddress
public InetSocketAddress() -
InetSocketAddress
public InetSocketAddress(int port)Creates a socket endpoint with the given port numberportand 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
Creates a socket endpoint with the given port numberportandaddress. The range for valid port numbers is between 0 and 65535 inclusive. Ifaddressisnullthis 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
Creates a socket endpoint with the given port numberportand the hostnamehost. The hostname is tried to be resolved and cannot benull. 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
Creates anInetSocketAddresswithout trying to resolve the hostname into anInetAddress. 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 hostnamehostisnullor the port is not in the range between 0 and 65535.
-
getPort
public final int getPort()Returns this socket address' port. -
getAddress
Returns this socket address' address. -
getHostName
Returns the hostname, doing a reverse DNS lookup on theInetAddressif no hostname string was provided at construction time. UsegetHostString()to avoid the reverse DNS lookup. -
getHostString
Returns the hostname if known, or the result ofInetAddress.getHostAddress. UnlikegetHostName(), 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:
trueif this socket address is unresolved,falseotherwise.
-
toString
Returns a string containing the address (or the hostname for an unresolvedInetSocketAddress) and port number. For example:"www.google.com/74.125.224.115:80"or"/127.0.0.1:80". -
equals
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:
equalsin classObject- Parameters:
socketAddr- the object to be tested for equality.- Returns:
trueif this socket and the given socket objectsocketAddrare equal,falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public final int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-