List of usage examples for java.net ServerSocket close
public void close() throws IOException
From source file:org.mule.tck.PortUtils.java
/** * Check and log is a given port is available * // w w w.ja v a2 s. c o m * @param port the port number to check * @return true if the port is available, false otherwise */ public static boolean isPortFree(int port) { boolean portIsFree = true; ServerSocket server = null; try { server = new ServerSocket(port); } catch (IOException e) { portIsFree = false; } finally { if (server != null) { try { server.close(); } catch (IOException e) { // ignore } } } return portIsFree; }
From source file:org.apache.beam.sdk.io.hadoop.format.HadoopFormatIOElasticTest.java
@BeforeClass public static void startServer() throws NodeValidationException, IOException { ServerSocket serverSocket = new ServerSocket(0); int port = serverSocket.getLocalPort(); serverSocket.close(); elasticInMemPort = String.valueOf(port); ElasticEmbeddedServer.startElasticEmbeddedServer(); }
From source file:org.apache.hama.MiniBSPCluster.java
private static void randomPort(HamaConfiguration conf) { try {// w w w .ja v a2 s. c o m ServerSocket skt = new ServerSocket(0); int p = skt.getLocalPort(); skt.close(); conf.set(Constants.PEER_PORT, new Integer(p).toString()); conf.setInt(Constants.GROOM_RPC_PORT, p + 100); } catch (IOException ioe) { LOG.error("Can not find a free port for BSPPeer.", ioe); } }
From source file:org.jkcsoft.java.util.JavaHelper.java
/** * @param server//from ww w . j a v a 2 s .c o m */ public static void safeClose(ServerSocket server) { if (server != null) { try { server.close(); } catch (IOException e) { log.error("Upon closing", e); } } else { log.warn("Null ServerSocket sent to safeClose()"); } }
From source file:Main.java
/** * Unconditionally close a <code>ServerSocket</code>. * <p/>//from w ww . java 2s.com * Equivalent to {@link ServerSocket#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * <p/> * Example code: * <pre> * ServerSocket socket = null; * try { * socket = new ServerSocket(); * // process socket * socket.close(); * } catch (Exception e) { * // error handling * } finally { * IOUtils.closeQuietly(socket); * } * </pre> * * @param sock the ServerSocket to close, may be null or already closed * @since 2.2 */ public static void closeQuietly(ServerSocket sock) { if (sock != null) { try { sock.close(); } catch (IOException ioe) { // ignored } } }
From source file:VASSAL.tools.io.IOUtils.java
/** * Close a {@link ServerSocket} unconditionally. Equivalent to * calling <code>s.close()</code> when <code>s</code> is nonnull. * {@link IOException}s are swallowed, as there is generally * nothing that can be done about exceptions on closing. * * @param s a (possibly <code>null</code>) <code>ServerSocket</code> */// w w w. ja va2 s. c o m // FIXME: Remove in Java 1.6+, when ServerSocket implements Closeable public static void closeQuietly(ServerSocket s) { if (s == null) return; try { s.close(); } catch (IOException e) { // ignore } }
From source file:sf.net.experimaestro.manager.js.SSHServer.java
/** * Finds a free local socket port.//from w w w.j a v a 2s.co m * * @return a free local socket port. * @throws java.io.IOException */ public static int findFreeLocalPort() throws IOException { ServerSocket server = new ServerSocket(0); try { return server.getLocalPort(); } finally { server.close(); } }
From source file:org.qi4j.library.shiro.AbstractServletTestSupport.java
protected static int findFreePortOnIfaceWithPreference(final InetAddress address, final int prefered) throws IOException { ServerSocket server; if (prefered > 0) { server = new ServerSocket(prefered, 1, address); } else {/*from w w w . ja va2 s . c o m*/ server = new ServerSocket(0, 1, address); } int port = server.getLocalPort(); server.close(); return port; }
From source file:com.twosigma.beakerx.kernel.MagicKernelManager.java
public static Integer findFreePort() { ServerSocket socket = null; try {//from w w w.ja va 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:org.apache.sshd.PortForwardingTest.java
private static int getFreePort() throws Exception { ServerSocket s = new ServerSocket(0); try {/*w ww . j a v a 2 s. c o m*/ return s.getLocalPort(); } finally { s.close(); } }