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:com.netflix.explorers.helloworld.ExplorerAppTest.java

private static int getLocalPort() throws IOException {
    ServerSocket ss = new ServerSocket(0);
    ss.setReuseAddress(true);
    return ss.getLocalPort();
}

From source file:Main.java

/**
 * Checks to see if a specific port is available.
 * //from ww  w .  j  a va2 s  . c  o m
 * @Author From apache Mina project
 * 
 * @param port
 *            the port to check for availability
 */
public static boolean portAvailable(final int port) {
    if (port < 1 || port > 30000) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    java.net.ServerSocket ss = null;
    java.net.DatagramSocket ds = null;
    try {
        ss = new java.net.ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new java.net.DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (final java.io.IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (final java.io.IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 * Code from http://mina.apache.org//*  ww  w.ja v  a  2s .c o  m*/
 */
public static boolean available(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
        /* falls through */
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return false;
}

From source file:com.nokia.dempsy.mpcluster.zookeeper.ZookeeperTestServer.java

public static int findNextPort() throws IOException {
    // find an unused ehpemeral port
    InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true); // this allows the server port to be bound to even if it's in TIME_WAIT
    serverSocket.bind(inetSocketAddress);
    port = serverSocket.getLocalPort();//from   w ww  .j a  va  2s .com
    serverSocket.close();
    return port;
}

From source file:org.alexlg.bankit.Launcher.java

/**
 * Check if a tcp port is open on localhost interface.
 * @param port Port to check/*  w ww.  jav  a 2s  .c o m*/
 * @return True if port is open, false otherwise
 */
private static boolean isPortOpen(int port) {
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(port);
        socket.setReuseAddress(true);
    } catch (IOException e) {
        return false;
    } finally {
        // Clean up
        try {
            if (socket != null)
                socket.close();
        } catch (IOException e) {
        }
    }

    return true;
}

From source file:com.image32.demo.simpleapi.SimpleApiDemo.java

public static boolean checkPortAvailablity(int port) {

    ServerSocket ss = null;
    DatagramSocket ds = null;//from w w w.  j av a  2 s .  c om
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                return false;
            }
        }
    }
    return false;
}

From source file:gridool.communication.transport.nio.GridNioServer.java

private static Selector createSelector(int port) throws IOException {
    final Selector selector = SelectorProvider.provider().openSelector();

    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.configureBlocking(false);

    ServerSocket servSocket = serverChannel.socket();
    servSocket.setReuseAddress(true);
    servSocket.bind(new InetSocketAddress(port));

    serverChannel.register(selector, SelectionKey.OP_ACCEPT);

    if (LOG.isInfoEnabled()) {
        LOG.info("GridNioServer is started at port: " + port);
    }/*from w ww  .j  a  va 2 s .c  o m*/
    return selector;
}

From source file:com.twosigma.beakerx.kernel.MagicKernelManager.java

public static Integer findFreePort() {
    ServerSocket socket = null;
    try {/*  w  w  w  .  j  a v a 2  s .  c o m*/
        socket = new ServerSocket(0);
        socket.setReuseAddress(true);
        int port = socket.getLocalPort();
        try {
            socket.close();
        } catch (IOException e) {
        }
        return port;
    } catch (IOException e) {
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:AvailablePortFinder.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *///from ww  w. j  av  a2  s.c  om
public static boolean available(int port) {
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:com.centeractive.ws.server.SimpleServerTest.java

public static boolean isPortAvailable(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;/*from ww  w . j a v a  2  s  . c  o m*/
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}