Search in sources :

Example 46 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class Configuration method getSocketAddr.

/**
   * Get the socket address for <code>hostProperty</code> as a
   * <code>InetSocketAddress</code>. If <code>hostProperty</code> is
   * <code>null</code>, <code>addressProperty</code> will be used. This
   * is useful for cases where we want to differentiate between host
   * bind address and address clients should use to establish connection.
   *
   * @param hostProperty bind host property name.
   * @param addressProperty address property name.
   * @param defaultAddressValue the default value
   * @param defaultPort the default port
   * @return InetSocketAddress
   */
public InetSocketAddress getSocketAddr(String hostProperty, String addressProperty, String defaultAddressValue, int defaultPort) {
    InetSocketAddress bindAddr = getSocketAddr(addressProperty, defaultAddressValue, defaultPort);
    final String host = get(hostProperty);
    if (host == null || host.isEmpty()) {
        return bindAddr;
    }
    return NetUtils.createSocketAddr(host, bindAddr.getPort(), hostProperty);
}
Also used : InetSocketAddress(java.net.InetSocketAddress)

Example 47 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class NetUtils method createSocketAddrForHost.

/**
   * Create a socket address with the given host and port.  The hostname
   * might be replaced with another host that was set via
   * {@link #addStaticResolution(String, String)}.  The value of
   * hadoop.security.token.service.use_ip will determine whether the
   * standard java host resolver is used, or if the fully qualified resolver
   * is used.
   * @param host the hostname or IP use to instantiate the object
   * @param port the port number
   * @return InetSocketAddress
   */
public static InetSocketAddress createSocketAddrForHost(String host, int port) {
    String staticHost = getStaticResolution(host);
    String resolveHost = (staticHost != null) ? staticHost : host;
    InetSocketAddress addr;
    try {
        InetAddress iaddr = SecurityUtil.getByName(resolveHost);
        // address look like the original given host
        if (staticHost != null) {
            iaddr = InetAddress.getByAddress(host, iaddr.getAddress());
        }
        addr = new InetSocketAddress(iaddr, port);
    } catch (UnknownHostException e) {
        addr = InetSocketAddress.createUnresolved(host, port);
    }
    return addr;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 48 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class Server method bind.

public static void bind(ServerSocket socket, InetSocketAddress address, int backlog, Configuration conf, String rangeConf) throws IOException {
    try {
        IntegerRanges range = null;
        if (rangeConf != null) {
            range = conf.getRange(rangeConf, "");
        }
        if (range == null || range.isEmpty() || (address.getPort() != 0)) {
            socket.bind(address, backlog);
        } else {
            for (Integer port : range) {
                if (socket.isBound())
                    break;
                try {
                    InetSocketAddress temp = new InetSocketAddress(address.getAddress(), port);
                    socket.bind(temp, backlog);
                } catch (BindException e) {
                //Ignored
                }
            }
            if (!socket.isBound()) {
                throw new BindException("Could not find a free port in " + range);
            }
        }
    } catch (SocketException e) {
        throw NetUtils.wrapException(null, 0, address.getHostName(), address.getPort(), e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) IntegerRanges(org.apache.hadoop.conf.Configuration.IntegerRanges)

Example 49 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class StandardSocketFactory method createSocket.

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    Socket socket = createSocket();
    socket.connect(new InetSocketAddress(host, port));
    return socket;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 50 with InetSocketAddress

use of java.net.InetSocketAddress in project flink by apache.

the class ConnectionUtils method tryToConnect.

/**
	 *
	 * @param fromAddress The address to connect from.
	 * @param toSocket The socket address to connect to.
	 * @param timeout The timeout fr the connection.
	 * @param logFailed Flag to indicate whether to log failed attempts on info level
	 *                  (failed attempts are always logged on DEBUG level).
	 * @return True, if the connection was successful, false otherwise.
	 * @throws IOException Thrown if the socket cleanup fails.
	 */
private static boolean tryToConnect(InetAddress fromAddress, SocketAddress toSocket, int timeout, boolean logFailed) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Trying to connect to (" + toSocket + ") from local address " + fromAddress + " with timeout " + timeout);
    }
    try (Socket socket = new Socket()) {
        // port 0 = let the OS choose the port
        SocketAddress bindP = new InetSocketAddress(fromAddress, 0);
        // machine
        socket.bind(bindP);
        socket.connect(toSocket, timeout);
        return true;
    } catch (Exception ex) {
        String message = "Failed to connect from address '" + fromAddress + "': " + ex.getMessage();
        if (LOG.isDebugEnabled()) {
            LOG.debug(message, ex);
        } else if (logFailed) {
            LOG.info(message);
        }
        return false;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) IOException(java.io.IOException) LeaderRetrievalException(org.apache.flink.runtime.leaderretrieval.LeaderRetrievalException) UnknownHostException(java.net.UnknownHostException)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2586 Test (org.junit.Test)595 IOException (java.io.IOException)592 Socket (java.net.Socket)345 InetAddress (java.net.InetAddress)242 SocketAddress (java.net.SocketAddress)176 ServerSocket (java.net.ServerSocket)170 ArrayList (java.util.ArrayList)168 Configuration (org.apache.hadoop.conf.Configuration)140 ByteBuffer (java.nio.ByteBuffer)129 UnknownHostException (java.net.UnknownHostException)122 InputStream (java.io.InputStream)102 OutputStream (java.io.OutputStream)101 SocketChannel (java.nio.channels.SocketChannel)101 SocketException (java.net.SocketException)89 File (java.io.File)88 HashMap (java.util.HashMap)78 URI (java.net.URI)72 Proxy (java.net.Proxy)65 SocketTimeoutException (java.net.SocketTimeoutException)65