Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/java/net/InetSocketAddress#pubctors) \| [Methods](https://developer.android.com/reference/java/net/InetSocketAddress#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/net/InetSocketAddress#inhmethods)

# InetSocketAddress


`
public


class
InetSocketAddress
`


`


`

`

extends https://developer.android.com/reference/java/net/SocketAddress


`

`


`

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.net.SocketAddress](https://developer.android.com/reference/java/net/SocketAddress) ||
|   | ↳ | java.net.InetSocketAddress |

<br />

*** ** * ** ***

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:**

- `https://developer.android.com/reference/java/net/Socket`
- `https://developer.android.com/reference/java/net/ServerSocket`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/net/InetSocketAddress#InetSocketAddress(int)(int port) ` Creates a socket address where the IP address is the wildcard address and the port number a specified value. |
| ` https://developer.android.com/reference/java/net/InetSocketAddress#InetSocketAddress(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String hostname, int port) ` Creates a socket address from a hostname and a port number. |
| ` https://developer.android.com/reference/java/net/InetSocketAddress#InetSocketAddress(java.net.InetAddress,%20int)(https://developer.android.com/reference/java/net/InetAddress addr, int port) ` Creates a socket address from an IP address and a port number. |

| ### Public methods ||
|---|---|
| ` static https://developer.android.com/reference/java/net/InetSocketAddress` | ` https://developer.android.com/reference/java/net/InetSocketAddress#createUnresolved(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String host, int port) ` Creates an unresolved socket address from a hostname and a port number. |
| ` final boolean` | ` https://developer.android.com/reference/java/net/InetSocketAddress#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Compares this object against the specified object. |
| ` final https://developer.android.com/reference/java/net/InetAddress` | ` https://developer.android.com/reference/java/net/InetSocketAddress#getAddress()() ` Gets the `InetAddress`. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/InetSocketAddress#getHostName()() ` Gets the `hostname`. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/InetSocketAddress#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` | ` https://developer.android.com/reference/java/net/InetSocketAddress#getPort()() ` Gets the port number. |
| ` final int` | ` https://developer.android.com/reference/java/net/InetSocketAddress#hashCode()() ` Returns a hashcode for this socket address. |
| ` final boolean` | ` https://developer.android.com/reference/java/net/InetSocketAddress#isUnresolved()() ` Checks whether the address has been resolved or not. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/InetSocketAddress#toString()() ` Constructs a string representation of this InetSocketAddress. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### InetSocketAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `port` | `int`: The port number <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the port parameter is outside the specified range of valid port values. |

### InetSocketAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `hostname` | `String`: the Host name <br /> |
| `port` | `int`: The port number <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the port parameter is outside the range of valid port values, or if the hostname parameter is `null`. |
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager is present and permission to resolve the host name is denied. |

**See also:**

- `https://developer.android.com/reference/java/net/InetSocketAddress#isUnresolved()`

### InetSocketAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `addr` | `InetAddress`: The IP address <br /> |
| `port` | `int`: The port number <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the port parameter is outside the specified range of valid port values. |

## Public methods

### createUnresolved

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `host` | `String`: the Host name <br /> |
| `port` | `int`: The port number <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/InetSocketAddress` | an `InetSocketAddress` representing the unresolved socket address <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the port parameter is outside the range of valid port values, or if the hostname parameter is `null`. |

**See also:**

- `https://developer.android.com/reference/java/net/InetSocketAddress#isUnresolved()`

### equals

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final 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 and the port number
are compared.

Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
considered equal.

<br />

| Parameters ||
|---|---|
| `obj` | `Object`: the object to compare against. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if the objects are the same; `false` otherwise. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/InetAddress#equals(java.lang.Object)`

### getAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final InetAddress getAddress ()
```

Gets the `InetAddress`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/InetAddress` | the InetAddress or `null` if it is unresolved. <br /> |

### getHostName

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the hostname part of the address. <br /> |

### getHostString

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the hostname, or String representation of the address. <br /> |

### getPort

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int getPort ()
```

Gets the port number.

<br />

| Returns ||
|---|---|
| `int` | the port number. <br /> |

### hashCode

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int hashCode ()
```

Returns a hashcode for this socket address.

<br />

| Returns ||
|---|---|
| `int` | a hash code value for this socket address. <br /> |

### isUnresolved

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final boolean isUnresolved ()
```

Checks whether the address has been resolved or not.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if the hostname couldn't be resolved into an `InetAddress`. <br /> |

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Constructs a string representation of this InetSocketAddress.
This string is constructed by calling `https://developer.android.com/reference/java/net/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](https://developer.android.com/reference/java/net/InetSocketAddress#isUnresolved()),
`<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
`https://developer.android.com/reference/java/net/InetSocketAddress#getHostString()`, rather than parsing the string returned by this
`https://developer.android.com/reference/java/net/InetSocketAddress#toString()` method.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of this object. <br /> |