Example usage for java.net ServerSocket setReuseAddress

List of usage examples for java.net ServerSocket setReuseAddress

Introduction

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

Prototype

public void setReuseAddress(boolean on) throws SocketException 

Source Link

Document

Enable/disable the SocketOptions#SO_REUSEADDR SO_REUSEADDR socket option.

Usage

From source file:org.apache.geode.internal.net.SocketCreator.java

public ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr,
        List<GatewayTransportFilter> transportFilters, int socketBufferSize) throws IOException {
    if (transportFilters.isEmpty()) {
        return createServerSocket(nport, backlog, bindAddr, socketBufferSize);
    } else {//w w w.j  a  v a2 s  .co m
        printConfig();
        ServerSocket result = new TransportFilterServerSocket(transportFilters);
        result.setReuseAddress(true);
        // Set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        result.setReceiveBufferSize(socketBufferSize);
        try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
        } catch (BindException e) {
            BindException throwMe = new BindException(
                    LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1
                            .toLocalizedString(new Object[] { bindAddr, Integer.valueOf(nport) }));
            throwMe.initCause(e);
            throw throwMe;
        }
        return result;
    }
}

From source file:tv.phantombot.PhantomBot.java

public void checkPortAvailabity(int port) {
    ServerSocket serverSocket = null;
    try {/*from w w w . j ava  2s. c  om*/
        serverSocket = bindIP.isEmpty() ? new ServerSocket(port)
                : new ServerSocket(port, 1, java.net.InetAddress.getByName(bindIP));
        serverSocket.setReuseAddress(true);
    } catch (IOException e) {
        com.gmt2001.Console.err.println("Port is already in use: " + port);
        com.gmt2001.Console.err.println("Ensure that another copy of PhantomBot is not running.");
        com.gmt2001.Console.err
                .println("If another copy is not running, try to change baseport in ./config/botlogin.txt");
        com.gmt2001.Console.err.println("PhantomBot will now exit.");
        System.exit(0);
    } finally {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                com.gmt2001.Console.err.println("Unable to close port for testing: " + port);
                com.gmt2001.Console.err.println("PhantomBot will now exit.");
                System.exit(0);
            }
        }
    }
}

From source file:org.cloudata.core.tabletserver.TabletServer.java

public void init(CloudataConf conf) throws IOException {
    this.serverStartTime = new Date();
    this.testMode = conf.getBoolean("testmode", false);
    this.conf = conf;

    this.maxMajorCompactionThread = this.conf.getInt("tabletServer.maxMajorCompactionThread", 5);
    this.maxSplitThread = this.conf.getInt("tabletServer.maxSplitThread", 5);

    this.compactionExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(maxMajorCompactionThread);
    this.splitExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(maxSplitThread);
    this.actionExecutor = (ThreadPoolExecutor) Executors
            .newFixedThreadPool(this.conf.getInt("tabletServer.maxMinorCompactionThread", 10));

    this.maxTabletCount = conf.getInt("tabletServer.max.tablet.count", 2000);

    this.maxResultRecord = conf.getInt("client.max.resultRecord", 5000);

    this.maxMemoryCacheCapacity = conf.getLong("memory.maxColumnCacheCapacity", 200) * 1024 * 1024;

    this.fs = CloudataFileSystem.get(conf);

    if (fs == null || !fs.isReady()) {
        LOG.fatal("FileSystem is not ready. TabletServer shutdown");
        shutdown();/*from   w  w w .java2s .  c om*/
    }

    InetSocketAddress serverAddress = NetworkUtil.getAddress(
            InetAddress.getLocalHost().getHostName() + ":" + conf.getInt("tabletServer.port", 7001));

    this.hostName = serverAddress.getHostName() + ":" + serverAddress.getPort();

    this.threadGroup = new ThreadGroup("TabletServer_" + hostName);

    this.leaseHolder = new LeaseHolder(threadGroup);

    this.tabletServerLockPath = Constants.SERVER + "/" + hostName;

    this.zk = LockUtil.getZooKeeper(conf, hostName, this);

    //<Split Lock >
    try {
        LockUtil.delete(zk, LockUtil.getZKPath(conf, Constants.TABLETSERVER_SPLIT + "/" + hostName), true);
    } catch (Exception e) {
        throw new IOException(e);
    }
    //</Split Lock >

    schemaMap = new TableSchemaMap(conf, zk);

    tabletServerMetrics = new TabletServerMetrics(conf, this);

    this.server = CRPC.getServer(zk, this, serverAddress.getHostName(), serverAddress.getPort(),
            conf.getInt("tabletServer.handler.count", 10), false, conf, tabletServerMetrics);

    ServerSocket ss = null;
    int port = conf.getInt("tabletServer.scanner.port", 50100);
    String bindAddress = "0.0.0.0";

    try {
        //      LOG.info("Opened Scanner Handler at " + hostName  + ", port=" + port);
        ss = new ServerSocket(port, 0, InetAddress.getByName(bindAddress));
        ss.setReuseAddress(true);
    } catch (IOException ie) {
        LOG.error("Could not open scanner server at " + port + ", stop server and Stop tablet server", ie);
        exit();
    }
    this.dataXceiveServer = new Daemon(new DataXceiveServer(ss));

    try {
        LockUtil.createNodes(zk, LockUtil.getZKPath(conf, tabletServerLockPath), hostName.getBytes(),
                CreateMode.EPHEMERAL);
        LOG.info("TableServer lock created:" + LockUtil.getZKPath(conf, tabletServerLockPath));
    } catch (Exception e) {
        LOG.fatal("TabletServer stopped. Can't server lock:" + tabletServerLockPath, e);
        exit();
    }

    if (tabletDistributionMode.get()) {
        LOG.info("Turn on tablet distribution mode");
    }

    heartbeatThread = new HeartbeatThread();
    heartbeatThread.setDaemon(true);
    heartbeatThread.start();
}