Java Socket Address Get getConnectAddress(InetSocketAddress addr)

Here you can find the source of getConnectAddress(InetSocketAddress addr)

Description

Returns InetSocketAddress that a client can use to connect to the server.

License

Apache License

Parameter

Parameter Description
addr of a listener

Return

socket address that a client can use to connect to the server.

Declaration

public static InetSocketAddress getConnectAddress(InetSocketAddress addr) 

Method Source Code

//package com.java2s;
/**/*from w  w w  .j a v  a 2  s.com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.*;

public class Main {
    /**
     * Returns InetSocketAddress that a client can use to
     * connect to the server. NettyServerBase.getListenerAddress() is not correct when
     * the server binds to "0.0.0.0". This returns "hostname:port" of the server,
     * or "127.0.0.1:port" when the getListenerAddress() returns "0.0.0.0:port".
     *
     * @param addr of a listener
     * @return socket address that a client can use to connect to the server.
     */
    public static InetSocketAddress getConnectAddress(InetSocketAddress addr) {
        if (!addr.isUnresolved() && addr.getAddress().isAnyLocalAddress()) {
            try {
                addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort());
            } catch (UnknownHostException uhe) {
                // shouldn't get here unless the host doesn't have a loopback iface
                addr = new InetSocketAddress("127.0.0.1", addr.getPort());
            }
        }
        InetSocketAddress canonicalAddress = new InetSocketAddress(addr.getAddress().getCanonicalHostName(),
                addr.getPort());
        return canonicalAddress;
    }
}

Related

  1. getAddrString(final SocketAddress address)
  2. getAlreadyBoundServerSocket(String address, int port, boolean ssl, boolean useChannels)
  3. getBestAddress(InetSocketAddress addr)
  4. getCanonicalConnectionAddress(InetSocketAddress address)
  5. getClientPort(SocketAddress socketAddress)
  6. getConnString(SocketAddress address)
  7. getDestination(InetSocketAddress server)
  8. getDestinationInetSocketAddress(URI uri, int defaultPort)
  9. getHostAddress(InetSocketAddress addr)