Java Utililty Methods Socket Address Get

List of utility methods to do Socket Address Get

Description

The list of methods to do Socket Address Get are organized into topic(s).

Method

InetSocketAddressgetConnectAddress(InetSocketAddress addr)
Returns InetSocketAddress that a client can use to connect to the server.
if (!addr.isUnresolved() && addr.getAddress().isAnyLocalAddress()) {
    try {
        addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort());
    } catch (UnknownHostException uhe) {
        addr = new InetSocketAddress("127.0.0.1", addr.getPort());
InetSocketAddress canonicalAddress = new InetSocketAddress(addr.getAddress().getCanonicalHostName(),
...
StringgetConnString(SocketAddress address)
get Conn String
String addrStr = address.toString();
if (addrStr.contains("/")) {
    addrStr = addrStr.substring(addrStr.indexOf("/") + 1);
return addrStr;
InetSocketAddressgetDestination(InetSocketAddress server)
get Destination
if (server.getAddress().isAnyLocalAddress()) {
    server = new InetSocketAddress(InetAddress.getLoopbackAddress(), server.getPort());
return server;
InetSocketAddressgetDestinationInetSocketAddress(URI uri, int defaultPort)
Get a socket address for a URI destination, resolving the host name if necessary.
final String host = uri.getHost();
if (host == null) {
    return null;
final int length = host.length();
if (length == 0) {
    return null;
int port = uri.getPort();
if (port == -1)
    port = defaultPort;
if (port == -1) {
    return null;
return new InetSocketAddress(host, port);
StringgetHostAddress(InetSocketAddress addr)
Returns the address of the host minimizing DNS lookups.
String hostToAppend = "";
if (addr.isUnresolved()) {
    hostToAppend = addr.getHostName();
} else {
    hostToAppend = addr.getAddress().getHostAddress();
return hostToAppend;
StringgetHostAddress(InetSocketAddress address)
get Host Address
if (address.isUnresolved()) {
    return (address.getHostName());
} else {
    return (address.getAddress().getHostAddress());
StringgetHostName(SocketAddress socketAddress)
get Host Name
if (socketAddress == null) {
    return null;
if (socketAddress instanceof InetSocketAddress) {
    InetAddress addr = ((InetSocketAddress) socketAddress).getAddress();
    if (addr != null) {
        return addr.getHostAddress();
return null;
StringgetHostNameNoResolve(InetSocketAddress address)
get Host Name No Resolve
InetAddress i_address = address.getAddress();
if (i_address == null) {
    return (address.getHostName());
} else {
    String str = i_address.toString();
    int pos = str.indexOf('/');
    if (pos == -1) {
        System.out.println("InetAddress::toString not returning expected result: " + str);
...
StringgetHostPortString(InetSocketAddress addr)
Compose a "host:port" string from the address.
return addr.getHostName() + ":" + addr.getPort();
StringgetHostPortString(InetSocketAddress addr)
Compose a "host:port" string from the address.
return addr.getHostName() + ":" + addr.getPort();