public class InetSocketAddress extends SocketAddress
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.
Socket
,
ServerSocket
,
Serialized FormConstructor and Description |
---|
InetSocketAddress(InetAddress addr,
int port)
Creates a socket address from an IP address and a port number.
|
InetSocketAddress(int port)
Creates a socket address where the IP address is the wildcard address
and the port number a specified value.
|
InetSocketAddress(String hostname,
int port)
Creates a socket address from a hostname and a port number.
|
Modifier and Type | Method and Description |
---|---|
static InetSocketAddress |
createUnresolved(String host,
int port)
Creates an unresolved socket address from a hostname and a port number.
|
boolean |
equals(Object obj)
Compares this object against the specified object.
|
InetAddress |
getAddress()
Gets the
InetAddress . |
String |
getHostName()
Gets the
hostname . |
String |
getHostString()
Returns the hostname, or the String form of the address if it
doesn't have a hostname (it was created using a literal).
|
int |
getPort()
Gets the port number.
|
int |
hashCode()
Returns a hashcode for this socket address.
|
boolean |
isUnresolved()
Checks whether the address has been resolved or not.
|
String |
toString()
Constructs a string representation of this InetSocketAddress.
|
public InetSocketAddress(int port)
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.
port
- The port numberIllegalArgumentException
- if the port parameter is outside the specified
range of valid port values.public InetSocketAddress(InetAddress addr, int port)
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.
addr
- The IP addressport
- The port numberIllegalArgumentException
- if the port parameter is outside the specified
range of valid port values.public InetSocketAddress(String hostname, int port)
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 permission
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.
hostname
- the Host nameport
- The port numberIllegalArgumentException
- if the port parameter is outside the range
of valid port values, or if the hostname parameter is null.SecurityException
- if a security manager is present and
permission to resolve the host name is
denied.isUnresolved()
public static InetSocketAddress createUnresolved(String host, int port)
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.
host
- the Host nameport
- The port numberInetSocketAddress
representing the unresolved
socket addressIllegalArgumentException
- if the port parameter is outside
the range of valid port values, or if the hostname
parameter is null.isUnresolved()
public final int getPort()
public final InetAddress getAddress()
InetAddress
.null
if it is unresolved.public final String getHostName()
hostname
.
Note: This method may trigger a name service reverse lookup if the
address was created with a literal IP address.public final String getHostString()
public final boolean isUnresolved()
true
if the hostname couldn't be resolved into
an InetAddress
.public String toString()
public final boolean equals(Object obj)
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 and the port number
are compared.
Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
considered equal.
equals
in class Object
obj
- the object to compare against.true
if the objects are the same;
false
otherwise.InetAddress.equals(java.lang.Object)
public final int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2022, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.