Top Description Inners Fields Constructors Methods
java.net

public Class InetSocketAddress

extends SocketAddress
Class Inheritance
Imports
java.io.IOException, .InvalidObjectException, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamException, .ObjectStreamField, java.util.Locale

This class implements an IP Socket Address (IP address + port number) It can also be a pair (hostname + port number), in which case an attempt will be made to resolve the hostname. If resolution fails then the address is said to be unresolved but can still be used on some circumstances like connecting through a proxy.

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.

Since
1.4
See Also
java.net.Socket, java.net.ServerSocket

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final long
private final transient InetSocketAddress.InetSocketAddressHolder
private static final ObjectStreamField[]
private static final long
private static final Unsafe

Constructor Summary

AccessConstructor and Description
public
InetSocketAddress(int
The port number
port
)

Creates a socket address where the IP address is the wildcard address and the port number a specified value.

public
InetSocketAddress(InetAddress
The IP address
addr
,
int
The port number
port
)

Creates a socket address from an IP address and a port number.

public
InetSocketAddress(String
the Host name
hostname
,
int
The port number
port
)

Creates a socket address from a hostname and a port number.

private
InetSocketAddress(int port, String hostname)

Method Summary

Modifier and TypeMethod and Description
private static String
checkHost(String hostname)

private static int
checkPort(int port)

public static InetSocketAddress

Returns:

an InetSocketAddress representing the unresolved socket address
createUnresolved
(String
the Host name
host
,
int
The port number
port
)

Creates an unresolved socket address from a hostname and a port number.

public final boolean

Returns:

true if the objects are the same; false otherwise.
equals
(Object
the object to compare against.
obj
)

Overrides java.lang.Object.equals.

Compares this object against the specified object.

public final InetAddress

Returns:

the InetAddress or null if it is unresolved.
getAddress
()

Gets the InetAddress.

public final String

Returns:

the hostname part of the address.
getHostName
()

Gets the hostname.

public final String

Returns:

the hostname, or String representation of the address.
getHostString
()

Returns the hostname, or the String form of the address if it doesn't have a hostname (it was created using a literal).

public final int

Returns:

the port number.
getPort
()

Gets the port number.

public final int

Returns:

a hash code value for this socket address.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hashcode for this socket address.

public final boolean

Returns:

true if the hostname couldn't be resolved into an InetAddress.
isUnresolved
()

Checks whether the address has been resolved or not.

private void
readObject(ObjectInputStream
the ObjectInputStream from which data is read
in
)

Restores the state of this object from the stream.

private void
readObjectNoData()

Throws InvalidObjectException, always.

public String

Returns:

a string representation of this object.
toString
()

Overrides java.lang.Object.toString.

Constructs a string representation of this InetSocketAddress.

private void
writeObject(ObjectOutputStream
the ObjectOutputStream to which data is written
out
)

Writes the state of this object to the stream.

Field Detail

FIELDS_OFFSETback to summary
private static final long FIELDS_OFFSET
holderback to summary
private final transient InetSocketAddress.InetSocketAddressHolder holder
serialPersistentFieldsback to summary
private static final ObjectStreamField[] serialPersistentFields
Annotations
@Serial
Serial Fields:
hostname:String
the hostname of the Socket Address
addr:InetAddress
the IP address of the Socket Address
port:int
the port number of the Socket Address
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.net.SocketAddress.serialVersionUID.

Annotations
@Serial
UNSAFEback to summary
private static final Unsafe UNSAFE

Constructor Detail

InetSocketAddressback to summary
public InetSocketAddress(int port)

Creates a socket address where the IP address is the wildcard address and the port number a specified value.

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.

Parameters
port:int

The port number

Exceptions
IllegalArgumentException:
if the port parameter is outside the specified range of valid port values.
InetSocketAddressback to summary
public InetSocketAddress(InetAddress addr, int port)

Creates a socket address from an IP address and a port number.

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.

Parameters
addr:InetAddress

The IP address

port:int

The port number

Exceptions
IllegalArgumentException:
if the port parameter is outside the specified range of valid port values.
InetSocketAddressback to summary
public InetSocketAddress(String hostname, int port)

Creates a socket address from a hostname and a port number.

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.

Parameters
hostname:String

the Host name

port:int

The port number

Exceptions
IllegalArgumentException:
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.
See Also
isUnresolved()
InetSocketAddressback to summary
private InetSocketAddress(int port, String hostname)

Method Detail

checkHostback to summary
private static String checkHost(String hostname)
checkPortback to summary
private static int checkPort(int port)
createUnresolvedback to summary
public static InetSocketAddress createUnresolved(String host, int port)

Creates an unresolved socket address from a hostname and a port number.

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.

Parameters
host:String

the Host name

port:int

The port number

Returns:InetSocketAddress

an InetSocketAddress representing the unresolved socket address

Exceptions
IllegalArgumentException:
if the port parameter is outside the range of valid port values, or if the hostname parameter is null.
Since
1.5
See Also
isUnresolved()
equalsback to summary
public final boolean equals(Object obj)

Overrides java.lang.Object.equals.

Compares this object against the specified object. The result is 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.

Parameters
obj:Object

the object to compare against.

Returns:boolean

true if the objects are the same; false otherwise.

Annotations
@Override
See Also
java.net.InetAddress#equals(java.lang.Object)
getAddressback to summary
public final InetAddress getAddress()

Gets the InetAddress.

Returns:InetAddress

the InetAddress or null if it is unresolved.

getHostNameback to summary
public final String getHostName()

Gets the hostname.

Note

This method may trigger a name service reverse lookup if the address was created with a literal IP address.

Returns:String

the hostname part of the address.

getHostStringback to summary
public final 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). This has the benefit of not attempting a reverse lookup.

Returns:String

the hostname, or String representation of the address.

Since
1.7
getPortback to summary
public final int getPort()

Gets the port number.

Returns:int

the port number.

hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hashcode for this socket address.

Returns:int

a hash code value for this socket address.

Annotations
@Override
isUnresolvedback to summary
public final boolean isUnresolved()

Checks whether the address has been resolved or not.

Returns:boolean

true if the hostname couldn't be resolved into an InetAddress.

readObjectback to summary
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException

Restores the state of this object from the stream.

Parameters
in:ObjectInputStream

the ObjectInputStream from which data is read

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
ClassNotFoundException:
if a serialized class cannot be loaded
readObjectNoDataback to summary
private void readObjectNoData() throws ObjectStreamException

Throws InvalidObjectException, always.

Annotations
@Serial
Exceptions
ObjectStreamException:
always
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Constructs a string representation of this InetSocketAddress. This string is constructed by calling InetAddress#toString() on the InetAddress and concatenating the port number (with a colon).

If the address is an IPv6 address, the IPv6 literal is enclosed in square brackets, for example: "localhost/[0:0:0:0:0:0:0:1]:80". If the address is unresolved, <unresolved> is displayed in place of the address literal, for example "foo/<unresolved>:80".

To retrieve a string representation of the hostname or the address, use getHostString(), rather than parsing the string returned by this toString() method.

Returns:String

a string representation of this object.

Annotations
@Override
writeObjectback to summary
private void writeObject(ObjectOutputStream out) throws IOException

Writes the state of this object to the stream.

Parameters
out:ObjectOutputStream

the ObjectOutputStream to which data is written

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
java.net back to summary

private final Class InetSocketAddress.InetSocketAddressHolder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final InetAddress
private final String
private final int

Constructor Summary

AccessConstructor and Description
private
InetSocketAddressHolder(String hostname, InetAddress addr, int port)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

private InetAddress
private String
private String
private int
public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

private boolean
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

addrback to summary
private final InetAddress addr
hostnameback to summary
private final String hostname
portback to summary
private final int port

Constructor Detail

InetSocketAddressHolderback to summary
private InetSocketAddressHolder(String hostname, InetAddress addr, int port)

Method Detail

equalsback to summary
public final boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAddressback to summary
private InetAddress getAddress()
getHostNameback to summary
private String getHostName()
getHostStringback to summary
private String getHostString()
getPortback to summary
private int getPort()
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isUnresolvedback to summary
private boolean isUnresolved()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override