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.wso2.carbon.device.mgt.iot.agent.firealarm.transport.TransportUtils.java

private static boolean checkIfPortAvailable(int port) {
    ServerSocket tcpSocket = null;
    DatagramSocket udpSocket = null;

    try {//from  w w  w.ja  va2 s. c o m
        tcpSocket = new ServerSocket(port);
        tcpSocket.setReuseAddress(true);

        udpSocket = new DatagramSocket(port);
        udpSocket.setReuseAddress(true);
        return true;
    } catch (IOException ex) {
        // denotes the port is in use
    } finally {
        if (tcpSocket != null) {
            try {
                tcpSocket.close();
            } catch (IOException e) {
                /* not to be thrown */
            }
        }

        if (udpSocket != null) {
            udpSocket.close();
        }
    }

    return false;
}

From source file:acoli.controller.Controller.java

/**
 * http://stackoverflow.com/questions/434718/sockets-discover-port-availability-using-java
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *///from w  w w . j a  v a2s.co m
public static boolean portAvailable(int port) {
    int MIN_PORT_NUMBER = 1000;
    int MAX_PORT_NUMBER = 9999;
    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:org.messic.service.MessicMain.java

/**
 * From apache camel Checks to see if a specific port is available.
 * /*from  w ww . jav  a 2s . c o  m*/
 * @param port the port to check for availability
 */
public static boolean isPortAvailable(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) {
    }

    finally {
        if (ds != null) {
            ds.close();
        }

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

    return false;
}

From source file:org.codemucker.testserver.TestServer.java

public static int findFreePort(String hostNameOrIP) throws UnknownHostException {
    if (hostNameOrIP != null && hostNameOrIP.trim().length() == 0) {
        hostNameOrIP = null;/*from  w w w  .  j  a va  2s . co  m*/
    }
    ServerSocket sock = null;
    try {
        // only need a single connection to find a free port
        sock = new ServerSocket(0, 1, hostNameOrIP == null ? null : InetAddress.getByName(hostNameOrIP));
        // free this port up pronto when we close it
        sock.setReuseAddress(true);
        return sock.getLocalPort();
    } catch (final IOException e) {
        throw new RuntimeException(
                "Can't free port for host " + hostNameOrIP + "(null means to find any local host)", e);
    } finally {
        if (sock != null) {
            try {
                sock.close();
            } catch (final IOException e) {
                // ignore, we don't really care
            }
        }
    }
}

From source file:org.kawanfw.sql.tomcat.TomcatStarterUtil.java

/**
 * Checks to see if a specific port is available.
 * //from w  w  w.ja  v a  2s.  c om
 * @param port
 *            the port to check for availability
 */
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) {
        // e.printStackTrace();
    } 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.twosigma.beaker.core.Main.java

private static boolean portAvailable(int port) {

    ServerSocket ss = null;
    try {//w ww .j  a  va 2 s .com
        InetAddress address = InetAddress.getByName("127.0.0.1");
        ss = new ServerSocket(port, 1, address);
        ss.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.eclipse.scanning.test.scan.nexus.MandelbrotRemoteTest.java

/**
 * Checks if a port is free.//  w w  w.ja  va 2  s  . c  om
 * @param port
 * @return
 */
public static boolean isPortFree(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) {
        // Swallow this, it's not free
        return false;
    } finally {
        if (ds != null) {
            ds.close();
        }

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

}

From source file:net.mybox.mybox.Common.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *///from w  w w  .  j ava 2  s. com
public static boolean portAvailable(int port) {
    if (port < 0 || port > 65535) {
        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:org.apache.hama.util.BSPNetUtils.java

/**
 * Checks to see if a specific port is available.
 * /*from  w  w  w .j  a va  2s  .  co m*/
 * @param port the port to check for availability
 */
public static boolean available(int port) {
    if (port < 1000 || 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.l2jfree.network.mmocore.AcceptorThread.java

public void openServerSocket(InetAddress address, int port) throws IOException {
    ServerSocketChannel selectable = ServerSocketChannel.open();
    selectable.configureBlocking(false);

    ServerSocket ss = selectable.socket();
    ss.setReuseAddress(true);
    ss.setReceiveBufferSize(getBufferSize());
    if (address == null) {
        ss.bind(new InetSocketAddress(port));
    } else {/*from ww  w.j av  a  2s  .co  m*/
        ss.bind(new InetSocketAddress(address, port));
    }
    selectable.register(getSelector(), SelectionKey.OP_ACCEPT);
}