List of usage examples for java.net ServerSocket close
public void close() throws IOException
From source file:eu.wordnice.wnconsole.WNCUtils.java
/*** PROTECTED ***/ protected static Handler.OneVoidHandler<Logger> createOnDisable(final ServerSocket sock) { return new Handler.OneVoidHandler<Logger>() { @Override//from w ww .j a va2 s . co m public void handle(Logger lg) { try { sock.close(); lg.info("WNConsole was disabled!"); } catch (Throwable tr) { lg.severe("Ups, cannot close server service! Details:"); tr.printStackTrace(); } } }; }
From source file:Main.java
/** * Checks to see if a specific port is available. * /*w w w .ja v a 2 s. c om*/ * @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:org.zodiark.publisher.PublisherTest.java
public final static int findFreePort() { ServerSocket socket = null; try {//from ww w . ja v a 2s . com socket = new ServerSocket(0); return socket.getLocalPort(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } return 8080; }
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///from ww w . j a v a 2 s. 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.image32.demo.simpleapi.SimpleApiDemo.java
public static boolean checkPortAvailablity(int port) { ServerSocket ss = null; DatagramSocket ds = null;//w ww . j av a 2 s. com 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:org.overture.ct.ctruntime.tests.CtTestCaseBase.java
public static int findAvailablePort(int fromPort, int toPort) { if (fromPort > toPort) { throw new IllegalArgumentException("startPortShouldBeLessThanOrEqualToEndPort"); }// w w w.j av a 2 s .c om int port = fromPort; ServerSocket socket = null; while (port <= toPort) { try { socket = new ServerSocket(port); return port; } catch (IOException e) { ++port; } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } return -1; }
From source file:com.baomidou.mybatisplus.toolkit.IOUtils.java
/** * Closes a <code>ServerSocket</code> unconditionally. * <p>/*from w ww. j a v a 2 s .co m*/ * Equivalent to {@link ServerSocket#close()}, except any exceptions will be ignored. This is typically used in * finally blocks. * <p> * Example code: * <p> * <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(final ServerSocket sock) { if (sock != null) { try { sock.close(); } catch (final IOException ioe) { logger.error("error close io", ioe); } } }
From source file:com.ngdata.hbaseindexer.impl.IndexerModelImplTest.java
public static int getFreePort() { ServerSocket socket = null; try {/*from w w w . j a v a 2 s .co m*/ socket = new ServerSocket(0); return socket.getLocalPort(); } catch (IOException e) { throw new RuntimeException("Error finding a free port", e); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { throw new RuntimeException("Error closing ServerSocket used to detect a free port.", e); } } } }
From source file:com.redblackit.web.server.HostNetUtils.java
/** * Check if port is available/* w w w.j a v a 2 s. c om*/ * * @param port * @return */ private static boolean isPortAvailable(int port) { ServerSocket serverSocket; try { serverSocket = new ServerSocket(); } catch (IOException ex) { throw new IllegalStateException("Unable to create ServerSocket.", ex); } try { InetSocketAddress sa = new InetSocketAddress(port); serverSocket.bind(sa); return true; } catch (IOException ex) { return false; } finally { try { serverSocket.close(); } catch (IOException ex) { // ignore } } }
From source file:TestHTTPSource.java
private static int findFreePort() throws IOException { ServerSocket socket = new ServerSocket(0); int port = socket.getLocalPort(); socket.close(); return port;/*from w w w . j a v a 2s . c o m*/ }