Example usage for java.net BindException BindException

List of usage examples for java.net BindException BindException

Introduction

In this page you can find the example usage for java.net BindException BindException.

Prototype

public BindException(String msg) 

Source Link

Document

Constructs a new BindException with the specified detail message as to why the bind error occurred.

Usage

From source file:org.apache.tomcat.util.net.PoolTcpEndpoint.java

public void initEndpoint() throws IOException, InstantiationException {
    try {//from w  ww .  j  a va2 s . c  o m
        if (factory == null)
            factory = ServerSocketFactory.getDefault();
        if (serverSocket == null) {
            try {
                if (inet == null) {
                    serverSocket = factory.createSocket(port, backlog);
                } else {
                    serverSocket = factory.createSocket(port, backlog, inet);
                }
            } catch (BindException be) {
                throw new BindException(be.getMessage() + ":" + port);
            }
        }
        if (serverTimeout >= 0)
            serverSocket.setSoTimeout(serverTimeout);
    } catch (IOException ex) {
        //       log("couldn't start endpoint", ex, Logger.DEBUG);
        throw ex;
    } catch (InstantiationException ex1) {
        //       log("couldn't start endpoint", ex1, Logger.DEBUG);
        throw ex1;
    }
    initialized = true;
}

From source file:org.cloudata.core.common.ipc.CServer.java

/**
 * A convience method to bind to a given address and report 
 * better exceptions if the address is not a valid host.
 * @param socket the socket to bind//  w  w  w  .j  av a 2s  .  c o  m
 * @param address the address to bind to
 * @param backlog the number of connections allowed in the queue
 * @throws BindException if the address can't be bound
 * @throws UnknownHostException if the address isn't a valid host name
 * @throws IOException other random errors from bind
 */
static void bind(ServerSocket socket, InetSocketAddress address, int backlog) throws IOException {
    try {
        socket.bind(address, backlog);
    } catch (BindException e) {
        throw new BindException("Problem binding to " + address + "," + e.getMessage());
    } catch (SocketException e) {
        // If they try to bind to a different host's address, give a better
        // error message.
        if ("Unresolved address".equals(e.getMessage())) {
            throw new UnknownHostException("Invalid hostname for server: " + address.getHostName());
        } else {
            throw e;
        }
    }
}

From source file:org.eredlab.g4.ccl.net.bsd.RCommandClient.java

InputStream _createErrorStream() throws IOException {
    int localPort;
    ServerSocket server;/* w w w .j  av a2  s. co m*/
    Socket socket;

    localPort = MAX_CLIENT_PORT;
    server = null; // Keep compiler from barfing

    for (localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; --localPort) {
        try {
            server = _socketFactory_.createServerSocket(localPort, 1, getLocalAddress());
        } catch (SocketException e) {
            continue;
        }
        break;
    }

    if (localPort < MIN_CLIENT_PORT)
        throw new BindException("All ports in use.");

    _output_.write(Integer.toString(server.getLocalPort()).getBytes());
    _output_.write('\0');
    _output_.flush();

    socket = server.accept();
    server.close();

    if (isRemoteVerificationEnabled() && !verifyRemote(socket)) {
        socket.close();
        throw new IOException("Security violation: unexpected connection attempt by "
                + socket.getInetAddress().getHostAddress());
    }

    return (new SocketInputStream(socket, socket.getInputStream()));
}

From source file:org.eredlab.g4.ccl.net.bsd.RCommandClient.java

/***
 * Opens a Socket connected to a remote host at the specified port and
 * originating from the specified local address using a port in a range
 * acceptable to the BSD rshell daemon.//from   ww w .  j  av  a2  s .c o  m
 * Before returning, {@link org.apache.commons.net.SocketClient#_connectAction_  _connectAction_() }
 * is called to perform connection initialization actions.
 * <p>
 * @param host  The remote host.
 * @param port  The port to connect to on the remote host.
 * @param localAddr  The local address to use.
 * @exception SocketException If the socket timeout could not be set.
 * @exception BindException If all acceptable rshell ports are in use.
 * @exception IOException If the socket could not be opened.  In most
 *  cases you will only want to catch IOException since SocketException is
 *  derived from it.
 ***/
public void connect(InetAddress host, int port, InetAddress localAddr)
        throws SocketException, BindException, IOException {
    int localPort;

    localPort = MAX_CLIENT_PORT;

    for (localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; --localPort) {
        try {
            _socket_ = _socketFactory_.createSocket(host, port, localAddr, localPort);
        } catch (SocketException e) {
            continue;
        }
        break;
    }

    if (localPort < MIN_CLIENT_PORT)
        throw new BindException("All ports in use or insufficient permssion.");

    _connectAction_();
}

From source file:org.openqa.selenium.server.SeleniumServer.java

/**
 * Starts the Jetty server//from w w w  . j a  va2s.co m
 *
 * @throws Exception on error.
 */
public void start() throws Exception {
    System.setProperty("org.openqa.jetty.http.HttpRequest.maxFormContentSize", "0"); // default max
                                                                                     // is 200k;
                                                                                     // zero is
                                                                                     // infinite
    try {
        server.start();
    } catch (MultiException e) {
        if (e.getExceptions().size() == 1 && e.getException(0) instanceof BindException) {
            throw new BindException(
                    "Selenium is already running on port " + getPort() + ". Or some other service is.");
        }
        throw e;
    }

    shutDownHook = new Thread(new ShutDownHook(this)); // Thread safety reviewed
    shutDownHook.setName("SeleniumServerShutDownHook");
    Runtime.getRuntime().addShutdownHook(shutDownHook);
}

From source file:tajo.webapp.HttpServer.java

/**
 * Start the server. Does not wait for the server to start.
 *//* w  w  w . ja  va 2s . c  o m*/
public void start() throws IOException {
    try {
        if (listenerStartedExternally) { // Expect that listener was started
                                         // securely
            if (listener.getLocalPort() == -1) // ... and verify
                throw new Exception("Exepected webserver's listener to be started " + "previously but wasn't");
            // And skip all the port rolling issues.
            webServer.start();
        } else {
            int port = 0;
            int oriPort = listener.getPort(); // The original requested port
            while (true) {
                try {
                    port = webServer.getConnectors()[0].getLocalPort();
                    LOG.debug("Port returned by webServer.getConnectors()[0]."
                            + "getLocalPort() before open() is " + port + ". Opening the listener on "
                            + oriPort);
                    listener.open();
                    port = listener.getLocalPort();
                    LOG.debug("listener.getLocalPort() returned " + listener.getLocalPort()
                            + " webServer.getConnectors()[0].getLocalPort() returned "
                            + webServer.getConnectors()[0].getLocalPort());
                    // Workaround to handle the problem reported in HADOOP-4744
                    if (port < 0) {
                        Thread.sleep(100);
                        int numRetries = 1;
                        while (port < 0) {
                            LOG.warn("listener.getLocalPort returned " + port);
                            if (numRetries++ > MAX_RETRIES) {
                                throw new Exception(" listener.getLocalPort is returning "
                                        + "less than 0 even after " + numRetries + " resets");
                            }
                            for (int i = 0; i < 2; i++) {
                                LOG.info("Retrying listener.getLocalPort()");
                                port = listener.getLocalPort();
                                if (port > 0) {
                                    break;
                                }
                                Thread.sleep(200);
                            }
                            if (port > 0) {
                                break;
                            }
                            LOG.info("Bouncing the listener");
                            listener.close();
                            Thread.sleep(1000);
                            listener.setPort(oriPort == 0 ? 0 : (oriPort += 1));
                            listener.open();
                            Thread.sleep(100);
                            port = listener.getLocalPort();
                        }
                    } // Workaround end
                    LOG.info("Jetty bound to port " + port);
                    webServer.start();
                    break;
                } catch (IOException ex) {
                    // if this is a bind exception,
                    // then try the next port number.
                    if (ex instanceof BindException) {
                        if (!findPort) {
                            BindException be = new BindException(
                                    "Port in use: " + listener.getHost() + ":" + listener.getPort());
                            be.initCause(ex);
                            throw be;
                        }
                    } else {
                        LOG.info("HttpServer.start() threw a non Bind IOException");
                        throw ex;
                    }
                } catch (MultiException ex) {
                    LOG.info("HttpServer.start() threw a MultiException");
                    throw ex;
                }
                listener.setPort((oriPort += 1));
            }
        }
        // Make sure there is no handler failures.
        Handler[] handlers = webServer.getHandlers();
        for (int i = 0; i < handlers.length; i++) {
            if (handlers[i].isFailed()) {
                throw new IOException("Problem in starting http server. Server handlers failed");
            }
        }
    } catch (IOException e) {
        throw e;
    } catch (InterruptedException e) {
        throw (IOException) new InterruptedIOException("Interrupted while starting HTTP server").initCause(e);
    } catch (Exception e) {
        throw new IOException("Problem starting http server", e);
    }
}