Skip to content

Most visited

Recently visited

navigation

InetSocketAddress

public class InetSocketAddress
extends SocketAddress

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


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.

See also:

Summary

Public constructors

InetSocketAddress(int port)

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

InetSocketAddress(InetAddress addr, int port)

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

InetSocketAddress(String hostname, int port)

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

Public methods

static InetSocketAddress createUnresolved(String host, int port)

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

final boolean equals(Object obj)

Compares this object against the specified object.

final InetAddress getAddress()

Gets the InetAddress.

final String getHostName()

Gets the hostname.

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).

final int getPort()

Gets the port number.

final int hashCode()

Returns a hashcode for this socket address.

final boolean isUnresolved()

Checks whether the address has been resolved or not.

String toString()

Constructs a string representation of this InetSocketAddress.

Inherited methods

From class java.lang.Object

Public constructors

InetSocketAddress

Added in API level 1
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
Throws
IllegalArgumentException if the port parameter is outside the specified range of valid port values.

InetSocketAddress

Added in API level 1
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
Throws
IllegalArgumentException if the port parameter is outside the specified range of valid port values.

InetSocketAddress

Added in API level 1
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 permissiom 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
Throws
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:

Public methods

createUnresolved

Added in API level 1
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 a InetSocketAddress representing the unresolved socket address
Throws
IllegalArgumentException if the port parameter is outside the range of valid port values, or if the hostname parameter is null.

See also:

equals

Added in API level 1
boolean equals (Object obj)

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 & 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.

See also:

getAddress

Added in API level 1
InetAddress getAddress ()

Gets the InetAddress.

Returns
InetAddress the InetAdress or null if it is unresolved.

getHostName

Added in API level 1
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.

getHostString

Added in API level 19
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.

getPort

Added in API level 1
int getPort ()

Gets the port number.

Returns
int the port number.

hashCode

Added in API level 1
int hashCode ()

Returns a hashcode for this socket address.

Returns
int a hash code value for this socket address.

isUnresolved

Added in API level 1
boolean isUnresolved ()

Checks whether the address has been resolved or not.

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

toString

Added in API level 1
String toString ()

Constructs a string representation of this InetSocketAddress. This String is constructed by calling toString() on the InetAddress and concatenating the port number (with a colon). If the address is unresolved then the part before the colon will only contain the hostname.

Returns
String a string representation of this object.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.