Search in sources :

Example 11 with BindException

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

the class TestIPv6NIOServerSocketChannel method bindNIOServerSocket.

/**
   * Creates a NIO ServerSocketChannel, and gets the ServerSocket from
   * there. Then binds the obtained socket.
   * This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a
   * IPv6 address. Works on Oracle JDK 1.7.
   */
private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
    while (true) {
        int port = HBaseTestingUtility.randomFreePort();
        InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
        ServerSocketChannel channel = null;
        ServerSocket serverSocket = null;
        try {
            channel = ServerSocketChannel.open();
            serverSocket = channel.socket();
            // This does not work
            serverSocket.bind(addr);
            break;
        } catch (BindException ex) {
        //continue
        } finally {
            if (serverSocket != null) {
                serverSocket.close();
            }
            if (channel != null) {
                channel.close();
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 12 with BindException

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

the class HRegionServer method putUpWebUI.

/**
   * Puts up the webui.
   * @return Returns final port -- maybe different from what we started with.
   * @throws IOException
   */
private int putUpWebUI() throws IOException {
    int port = this.conf.getInt(HConstants.REGIONSERVER_INFO_PORT, HConstants.DEFAULT_REGIONSERVER_INFOPORT);
    String addr = this.conf.get("hbase.regionserver.info.bindAddress", "0.0.0.0");
    if (this instanceof HMaster) {
        port = conf.getInt(HConstants.MASTER_INFO_PORT, HConstants.DEFAULT_MASTER_INFOPORT);
        addr = this.conf.get("hbase.master.info.bindAddress", "0.0.0.0");
    }
    // -1 is for disabling info server
    if (port < 0)
        return port;
    if (!Addressing.isLocalAddress(InetAddress.getByName(addr))) {
        String msg = "Failed to start http info server. Address " + addr + " does not belong to this host. Correct configuration parameter: " + "hbase.regionserver.info.bindAddress";
        LOG.error(msg);
        throw new IOException(msg);
    }
    // check if auto port bind enabled
    boolean auto = this.conf.getBoolean(HConstants.REGIONSERVER_INFO_PORT_AUTO, false);
    while (true) {
        try {
            this.infoServer = new InfoServer(getProcessName(), addr, port, false, this.conf);
            infoServer.addServlet("dump", "/dump", getDumpServlet());
            configureInfoServer();
            this.infoServer.start();
            break;
        } catch (BindException e) {
            if (!auto) {
                // auto bind disabled throw BindException
                LOG.error("Failed binding http info server to port: " + port);
                throw e;
            }
            // auto bind enabled, try to use another port
            LOG.info("Failed binding http info server to port: " + port);
            port++;
        }
    }
    port = this.infoServer.getPort();
    conf.setInt(HConstants.REGIONSERVER_INFO_PORT, port);
    int masterInfoPort = conf.getInt(HConstants.MASTER_INFO_PORT, HConstants.DEFAULT_MASTER_INFOPORT);
    conf.setInt("hbase.master.info.port.orig", masterInfoPort);
    conf.setInt(HConstants.MASTER_INFO_PORT, port);
    return port;
}
Also used : HMaster(org.apache.hadoop.hbase.master.HMaster) BindException(java.net.BindException) InfoServer(org.apache.hadoop.hbase.http.InfoServer) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 13 with BindException

use of java.net.BindException in project storm by apache.

the class Zookeeper method mkInprocessZookeeper.

public static List mkInprocessZookeeper(String localdir, Integer port) throws Exception {
    File localfile = new File(localdir);
    ZooKeeperServer zk = new ZooKeeperServer(localfile, localfile, 2000);
    NIOServerCnxnFactory factory = null;
    int report = 2000;
    int limitPort = 65535;
    if (port != null) {
        report = port;
        limitPort = port;
    }
    while (true) {
        try {
            factory = new NIOServerCnxnFactory();
            factory.configure(new InetSocketAddress(report), 0);
            break;
        } catch (BindException e) {
            report++;
            if (report > limitPort) {
                throw new RuntimeException("No port is available to launch an inprocess zookeeper");
            }
        }
    }
    LOG.info("Starting inprocess zookeeper at port {} and dir {}", report, localdir);
    factory.startup(zk);
    return Arrays.asList((Object) new Long(report), (Object) factory);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 14 with BindException

use of java.net.BindException in project screenbird by adamhub.

the class ServerIPC method initServerOrClient.

/**
     * Attempts to start a ServerSocket to a well-known-socket. If no 
     * BindException is thrown then ServerSocket creation was successful
     * and this instance is currently the first and only instance running. 
     * <br/><br/>
     * If the BindException is thrown, that means there is another previously
     * opened/running instance of Pastevid/ScreenRecorder running. Once
     * it is determined that this instance is not the first instance, the 
     * attempt to connect to the instance which is running the ServerSocket
     * is made. 
     * 
     * @return <b>True</b> if this Pastevid/Screenrecorder application is the 
     * first instance to be opened. <b>False</b> if it is determined that this
     * instance is not the first opened instance. 
     */
public final synchronized boolean initServerOrClient() {
    try {
        // Open Socket
        this.serverSocket = new ServerSocket(SOCKET_PORT);
        // Update signals
        this.setRunningStatus(true);
        this.ipc.setIsServerOrClient(true);
        log("Base Server is up and running");
        log("Found Server, resuming application");
        return true;
    } catch (BindException e) {
        log("Base Server is already Running");
        log("Found Client, Closing GUI");
        // Update signals
        this.setRunningStatus(false);
        // This is the second instance
        // Calling original server to view
        ClientIPC client = new ClientIPC();
        client.connect();
        client.close();
        // Set this thread to kill
        this.ipc.setIsServerOrClient(false);
    } catch (IOException e) {
        log(e);
    }
    // Kill process
    return false;
}
Also used : BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 15 with BindException

use of java.net.BindException in project screenbird by adamhub.

the class Server method init.

public void init() {
    try {
        //Open Socket
        this.serverSocket = new ServerSocket(SOCKET_PORT);
        //Update signals
        this.isRunning = true;
        System.out.println("Base Server is up and running");
        //Start server
        //this.serverManager.main();
        this.ipcProtocol.setServer(this);
    } catch (BindException e) {
        System.out.println("Base Server is already Running");
        //Kill process
        System.exit(0);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}
Also used : BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Aggregations

BindException (java.net.BindException)96 IOException (java.io.IOException)31 InetSocketAddress (java.net.InetSocketAddress)23 Test (org.junit.Test)22 ServerSocket (java.net.ServerSocket)21 File (java.io.File)11 SocketException (java.net.SocketException)10 Configuration (org.apache.hadoop.conf.Configuration)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 MiniDFSNNTopology (org.apache.hadoop.hdfs.MiniDFSNNTopology)5 InterruptedIOException (java.io.InterruptedIOException)4 InetAddress (java.net.InetAddress)4 RemoteException (java.rmi.RemoteException)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3