Example usage for java.net ServerSocket close

List of usage examples for java.net ServerSocket close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:test.integ.be.agiv.security.IPSTSTest.java

private static int getFreePort() throws Exception {
    ServerSocket serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    serverSocket.close();
    return port;//from w w  w.  j a v  a  2s  .c  om
}

From source file:org.openspaces.test.client.executor.ExecutorUtils.java

/**
 * Get an anonymous port./* w w w . ja  v  a 2s.co  m*/
 *
 * @return An anonymous port created by instantiating a <code>java.net.ServerSocket</code> with
 * a port of 0.
 */
public static int getAnonymousPort() {
    // default port
    int port = 45467;

    java.net.ServerSocket socket;
    try {
        socket = new java.net.ServerSocket(0);
        port = socket.getLocalPort();
        socket.close();
    } catch (IOException e) {
        if (_logger.isWarnEnabled())
            _logger.warn("Failed to get anonymous socket port.", e);
    }

    return port;
}

From source file:org.jumpmind.util.AppUtils.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port//from ww  w .  j  ava2 s . c o  m
 *            the port to check for availability
 */
public static boolean isPortAvailable(int port) {
    if (port < 1 || 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.synapse.SynapseHTTPServer.java

private static void selectPort(ConfigurationContext configCtx) {
    // check if configured port is available
    TransportInDescription trsIn = (TransportInDescription) configCtx.getAxisConfiguration().getTransportsIn()
            .get(new QName("http"));

    if (trsIn != null) {

        int port = 8080;

        String strPort = System.getProperty("port");
        if (strPort != null) {
            // port is specified as a VM parameter
            try {
                port = new Integer(strPort).intValue();
            } catch (NumberFormatException e) {
                // user supplied parameter is not a valid integer. so use the port in configuration.
                log.error(// ww  w .j  a v  a  2  s  .c o  m
                        "Given port is not a valid integer. Port specified in the configuration is used for the server.");
                port = Integer.parseInt(trsIn.getParameter("port").getValue().toString());
            }

        } else {
            port = Integer.parseInt(trsIn.getParameter("port").getValue().toString());
        }

        while (true) {
            ServerSocket sock = null;
            try {
                sock = new ServerSocket(port);
                trsIn.getParameter("port").setValue(Integer.toString(port));
                break;
            } catch (Exception e) {
                System.out.println("[SynapseHTTPServer] Port " + port + " already in use. Trying alternate");
                if (port == 8080) {
                    port = 8008;
                } else {
                    port++;
                }
            } finally {
                if (sock != null) {
                    try {
                        sock.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
}

From source file:org.ngrinder.recorder.util.NetworkUtil.java

/**
 * Get a available port.//www  . j  a va 2 s.  c o  m
 * 
 * @param localHostAddress
 *            localHostAddress
 * 
 * @return min port available from scanStartPort
 */
public static int getAvailablePort(InetAddress localHostAddress) {
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(0, 50, localHostAddress);
        return socket.getLocalPort();
    } catch (IOException e) {
        LOGGER.error("Error during openning port. {}", e.getMessage());
        LOGGER.debug("Details:{}", e.getMessage(), e);
        return 16000;
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                LOGGER.error("Error during closing port");
            }
        }
    }
}

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.j a  v  a  2 s  . com
        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:com.alibaba.jstorm.yarn.utils.JstormYarnUtils.java

/**
 * See if a port is available for listening on by trying to listen
 * on it and seeing if that works or fails.
 *
 * @param port port to listen to/*w w  w.j  a v a  2  s.  c o  m*/
 * @return true if the port was available for listening on
 */
public static boolean isPortAvailable(int port) {
    try {
        ServerSocket socket = new ServerSocket(port);
        socket.close();
        return true;
    } catch (IOException e) {
        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 av a 2  s.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.ngrinder.recorder.util.NetworkUtil.java

/**
 * Get the available port using the given default port. If it's not available, it chooses the
 * random port.//from  w  ww.j a  va  2s  .  c  o  m
 * 
 * @param localHostAddress
 *            address in which the port will be assigned.
 * @param defaultPort
 *            default port.
 * @return usable port.
 */
public static int getAvailablePort(InetAddress localHostAddress, int defaultPort) {
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(defaultPort, 50, localHostAddress);

        return socket.getLocalPort();
    } catch (IOException e) {
        LOGGER.error("Error during openning port. {}", e.getMessage());
        LOGGER.debug("Details:{}", e.getMessage(), e);
        return getAvailablePort(localHostAddress);
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                LOGGER.error("Error during port close");
            }
        }
    }
}

From source file:org.messic.service.MessicMain.java

/**
 * From apache camel Checks to see if a specific port is available.
 * //from w  w  w . ja  v  a 2 s  .  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;
}