InetSocketAddress (Java Platform SE 8 ) (original) (raw)
- java.net.SocketAddress
- java.net.InetSocketAddress
All Implemented Interfaces:
Serializable
public class InetSocketAddress
extends SocketAddress
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:
Socket, ServerSocket, Serialized Form
Constructor Summary
Constructors
Constructor 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. Method Summary
All Methods Static Methods Instance Methods Concrete Methods
Modifier and Type Method 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. * ### Methods inherited from class java.lang.[Object](../../java/lang/Object.html "class in java.lang") `[clone](../../java/lang/Object.html#clone--), [finalize](../../java/lang/Object.html#finalize--), [getClass](../../java/lang/Object.html#getClass--), [notify](../../java/lang/Object.html#notify--), [notifyAll](../../java/lang/Object.html#notifyAll--), [wait](../../java/lang/Object.html#wait--), [wait](../../java/lang/Object.html#wait-long-), [wait](../../java/lang/Object.html#wait-long-int-)`
Constructor Detail
* #### InetSocketAddress 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` \- The port number Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the port parameter is outside the specified range of valid port values. * #### InetSocketAddress public InetSocketAddress([InetAddress](../../java/net/InetAddress.html "class in java.net") 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` \- The IP address `port` \- The port number Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the port parameter is outside the specified range of valid port values. * #### InetSocketAddress public InetSocketAddress([String](../../java/lang/String.html "class in java.lang") 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` \- the Host name `port` \- The port number Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the port parameter is outside the range of valid port values, or if the hostname parameter is null. `[SecurityException](../../java/lang/SecurityException.html "class in java.lang")` \- if a security manager is present and permission to resolve the host name is denied. See Also: [isUnresolved()](../../java/net/InetSocketAddress.html#isUnresolved--)
Method Detail
* #### createUnresolved public static [InetSocketAddress](../../java/net/InetSocketAddress.html "class in java.net") createUnresolved([String](../../java/lang/String.html "class in java.lang") 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` \- the Host name `port` \- The port number Returns: a `InetSocketAddress` representing the unresolved socket address Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- 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()](../../java/net/InetSocketAddress.html#isUnresolved--) * #### getPort public final int getPort() Gets the port number. Returns: the port number. * #### getAddress public final [InetAddress](../../java/net/InetAddress.html "class in java.net") getAddress() Gets the `InetAddress`. Returns: the InetAdress or `null` if it is unresolved. * #### getHostName public final [String](../../java/lang/String.html "class in java.lang") 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: the hostname part of the address. * #### getHostString public final [String](../../java/lang/String.html "class in java.lang") 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: the hostname, or String representation of the address. Since: 1.7 * #### isUnresolved public final boolean isUnresolved() Checks whether the address has been resolved or not. Returns: `true` if the hostname couldn't be resolved into an `InetAddress`. * #### toString public [String](../../java/lang/String.html "class in java.lang") 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. Overrides: `[toString](../../java/lang/Object.html#toString--)` in class `[Object](../../java/lang/Object.html "class in java.lang")` Returns: a string representation of this object. * #### equals public final boolean equals([Object](../../java/lang/Object.html "class in java.lang") 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. Overrides: `[equals](../../java/lang/Object.html#equals-java.lang.Object-)` in class `[Object](../../java/lang/Object.html "class in java.lang")` Parameters: `obj` \- the object to compare against. Returns: `true` if the objects are the same;`false` otherwise. See Also: [InetAddress.equals(java.lang.Object)](../../java/net/InetAddress.html#equals-java.lang.Object-) * #### hashCode public final int hashCode() Returns a hashcode for this socket address. Overrides: `[hashCode](../../java/lang/Object.html#hashCode--)` in class `[Object](../../java/lang/Object.html "class in java.lang")` Returns: a hash code value for this socket address. See Also: [Object.equals(java.lang.Object)](../../java/lang/Object.html#equals-java.lang.Object-), [System.identityHashCode(java.lang.Object)](../../java/lang/System.html#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, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.