Search in sources :

Example 6 with UnknownHostException

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

the class DNS method getIPs.

/**
   * Returns all the IPs associated with the provided interface, if any, in
   * textual form.
   * 
   * @param strInterface
   *            The name of the network interface or sub-interface to query
   *            (eg eth0 or eth0:0) or the string "default"
   * @param returnSubinterfaces
   *            Whether to return IPs associated with subinterfaces of
   *            the given interface
   * @return A string vector of all the IPs associated with the provided
   *         interface. The local host IP is returned if the interface
   *         name "default" is specified or there is an I/O error looking
   *         for the given interface.
   * @throws UnknownHostException
   *             If the given interface is invalid
   * 
   */
public static String[] getIPs(String strInterface, boolean returnSubinterfaces) throws UnknownHostException {
    if ("default".equals(strInterface)) {
        return new String[] { cachedHostAddress };
    }
    NetworkInterface netIf;
    try {
        netIf = NetworkInterface.getByName(strInterface);
        if (netIf == null) {
            netIf = getSubinterface(strInterface);
        }
    } catch (SocketException e) {
        LOG.warn("I/O error finding interface " + strInterface + ": " + e.getMessage());
        return new String[] { cachedHostAddress };
    }
    if (netIf == null) {
        throw new UnknownHostException("No such interface " + strInterface);
    }
    // NB: Using a LinkedHashSet to preserve the order for callers
    // that depend on a particular element being 1st in the array.
    // For example, getDefaultIP always returns the first element.
    LinkedHashSet<InetAddress> allAddrs = new LinkedHashSet<InetAddress>();
    allAddrs.addAll(Collections.list(netIf.getInetAddresses()));
    if (!returnSubinterfaces) {
        allAddrs.removeAll(getSubinterfaceInetAddrs(netIf));
    }
    String[] ips = new String[allAddrs.size()];
    int i = 0;
    for (InetAddress addr : allAddrs) {
        ips[i++] = addr.getHostAddress();
    }
    return ips;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 7 with UnknownHostException

use of java.net.UnknownHostException 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 8 with UnknownHostException

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

the class ServerWebApp method resolveAuthority.

/**
   * Resolves the host and port InetSocketAddress the
   * web server is listening to.
   * <p>
   * This implementation looks for the following 2 properties:
   * <ul>
   *   <li>#SERVER_NAME#.http.hostname</li>
   *   <li>#SERVER_NAME#.http.port</li>
   * </ul>
   *
   * @return the host and port InetSocketAddress the
   *         web server is listening to.
   * @throws ServerException thrown
   *         if any of the above 2 properties is not defined.
   */
protected InetSocketAddress resolveAuthority() throws ServerException {
    String hostnameKey = getName() + HTTP_HOSTNAME;
    String portKey = getName() + HTTP_PORT;
    String host = System.getProperty(hostnameKey);
    String port = System.getProperty(portKey);
    if (host == null) {
        throw new ServerException(ServerException.ERROR.S13, hostnameKey);
    }
    if (port == null) {
        throw new ServerException(ServerException.ERROR.S13, portKey);
    }
    try {
        InetAddress add = InetAddress.getByName(host);
        int portNum = Integer.parseInt(port);
        return new InetSocketAddress(add, portNum);
    } catch (UnknownHostException ex) {
        throw new ServerException(ServerException.ERROR.S14, ex.toString(), ex);
    }
}
Also used : ServerException(org.apache.hadoop.lib.server.ServerException) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 9 with UnknownHostException

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

the class TaskAttemptImpl method resolveHost.

protected String resolveHost(String src) {
    // Fallback in case of failure.
    String result = src;
    try {
        InetAddress addr = InetAddress.getByName(src);
        result = addr.getHostName();
    } catch (UnknownHostException e) {
        LOG.warn("Failed to resolve address: " + src + ". Continuing to use the same.");
    }
    return result;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Example 10 with UnknownHostException

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

the class TestUdpServer method testRequest.

static void testRequest(XDR request, XDR request2) {
    try {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = request.getBytes();
        byte[] receiveData = new byte[65535];
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Nfs3Constant.SUN_RPCBIND);
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        clientSocket.close();
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: localhost.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: localhost.");
        System.exit(1);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Aggregations

UnknownHostException (java.net.UnknownHostException)780 InetAddress (java.net.InetAddress)319 IOException (java.io.IOException)229 InetSocketAddress (java.net.InetSocketAddress)88 Test (org.junit.Test)74 SocketException (java.net.SocketException)61 ArrayList (java.util.ArrayList)59 Socket (java.net.Socket)48 SocketTimeoutException (java.net.SocketTimeoutException)38 File (java.io.File)34 URL (java.net.URL)32 HashMap (java.util.HashMap)31 MalformedURLException (java.net.MalformedURLException)30 ConnectException (java.net.ConnectException)27 NetworkInterface (java.net.NetworkInterface)25 Properties (java.util.Properties)24 Inet4Address (java.net.Inet4Address)22 InputStream (java.io.InputStream)20 HttpURLConnection (java.net.HttpURLConnection)20 URI (java.net.URI)20