Search in sources :

Example 81 with SocketException

use of java.net.SocketException in project hbase by apache.

the class Addressing method getIpAddress.

private static InetAddress getIpAddress(AddressSelectionCondition condition) throws SocketException {
    // Before we connect somewhere, we cannot be sure about what we'd be bound to; however,
    // we only connect when the message where client ID is, is long constructed. Thus,
    // just use whichever IP address we can find.
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();
        if (!current.isUp() || current.isLoopback() || current.isVirtual())
            continue;
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();
            if (addr.isLoopbackAddress())
                continue;
            if (condition.isAcceptableAddress(addr)) {
                return addr;
            }
        }
    }
    throw new SocketException("Can't get our ip address, interfaces are: " + interfaces);
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 82 with SocketException

use of java.net.SocketException 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 83 with SocketException

use of java.net.SocketException 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 84 with SocketException

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

the class PrivilegedNfsGatewayStarter method init.

@Override
public void init(DaemonContext context) throws Exception {
    System.err.println("Initializing privileged NFS client socket...");
    NfsConfiguration conf = new NfsConfiguration();
    int clientPort = conf.getInt(NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY, NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_DEFAULT);
    if (clientPort < 1 || clientPort > 1023) {
        throw new RuntimeException("Must start privileged NFS server with '" + NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY + "' configured to a " + "privileged port.");
    }
    try {
        InetSocketAddress socketAddress = new InetSocketAddress("localhost", clientPort);
        registrationSocket = new DatagramSocket(null);
        registrationSocket.setReuseAddress(true);
        registrationSocket.bind(socketAddress);
    } catch (SocketException e) {
        LOG.error("Init failed for port=" + clientPort, e);
        throw e;
    }
    args = context.getArguments();
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) NfsConfiguration(org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration)

Example 85 with SocketException

use of java.net.SocketException in project nanohttpd by NanoHttpd.

the class ClientHandler method run.

@Override
public void run() {
    OutputStream outputStream = null;
    try {
        outputStream = this.acceptSocket.getOutputStream();
        ITempFileManager tempFileManager = httpd.getTempFileManagerFactory().create();
        HTTPSession session = new HTTPSession(httpd, tempFileManager, this.inputStream, outputStream, this.acceptSocket.getInetAddress());
        while (!this.acceptSocket.isClosed()) {
            session.execute();
        }
    } catch (Exception e) {
        // stacktrace
        if (!(e instanceof SocketException && "NanoHttpd Shutdown".equals(e.getMessage())) && !(e instanceof SocketTimeoutException)) {
            NanoHTTPD.LOG.log(Level.SEVERE, "Communication with the client broken, or an bug in the handler code", e);
        }
    } finally {
        NanoHTTPD.safeClose(outputStream);
        NanoHTTPD.safeClose(this.inputStream);
        NanoHTTPD.safeClose(this.acceptSocket);
        httpd.asyncRunner.closed(this);
    }
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) OutputStream(java.io.OutputStream) ITempFileManager(org.nanohttpd.protocols.http.tempfiles.ITempFileManager) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

SocketException (java.net.SocketException)523 IOException (java.io.IOException)189 InetAddress (java.net.InetAddress)127 NetworkInterface (java.net.NetworkInterface)110 Socket (java.net.Socket)100 UnknownHostException (java.net.UnknownHostException)93 ServerSocket (java.net.ServerSocket)82 DatagramSocket (java.net.DatagramSocket)78 SocketTimeoutException (java.net.SocketTimeoutException)77 InetSocketAddress (java.net.InetSocketAddress)62 Test (org.junit.Test)50 ConnectException (java.net.ConnectException)39 DatagramPacket (java.net.DatagramPacket)38 ArrayList (java.util.ArrayList)35 BindException (java.net.BindException)31 Inet4Address (java.net.Inet4Address)24 SocketAddress (java.net.SocketAddress)24 InterruptedIOException (java.io.InterruptedIOException)23 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)23 SmallTest (android.test.suitebuilder.annotation.SmallTest)20