List of usage examples for java.net ServerSocket close
public void close() throws IOException
From source file:org.apache.flume.test.util.SyslogAgent.java
public void setRandomPort() throws IOException { ServerSocket s = new ServerSocket(0); port = s.getLocalPort();/*from w w w . ja va 2 s .c o m*/ s.close(); }
From source file:org.sakaiproject.nakamura.testutils.http.DummyServer.java
/** * Create the dummy server on the first available port. There will be 100 tries before * we stop trying./* w w w . j a v a2s . c o m*/ */ public DummyServer() { int attempts = 0; while (server == null) { try { // new ServerSocket(0) will automatically try to find a free port. ServerSocket socket = new ServerSocket(0); port = socket.getLocalPort(); socket.close(); server = new Server(port); server.setHandler(this); server.start(); break; } catch (Exception e) { if (server != null) { try { server.stop(); server.destroy(); } catch (Exception ex2) { } } server = null; } attempts++; if (attempts == 100) { throw new RuntimeException( "Unable to find a free port the range 8888 - 8988, aborting http server startup "); } } }
From source file:org.paxml.el.UtilFunctions.java
/** * Get a random available port./*from w ww . j a v a2s.c o m*/ * @return the port, or minus if no available port found */ public static int getRandomPort() { ServerSocket s = null; try { s = new ServerSocket(0); return s.getLocalPort(); } catch (IOException e) { log.warn("Cannot get random port", e); return -1; } finally { try { if (s != null) { s.close(); } } catch (Exception e) { log.warn("Cannot close server socket of port: " + s.getLocalPort(), e); } } }
From source file:org.apache.apex.malhar.kafka.EmbeddedKafka.java
public void start() throws IOException { // Find port/*from w w w . jav a 2 s .co m*/ try { ServerSocket serverSocket = new ServerSocket(0); BROKERPORT = Integer.toString(serverSocket.getLocalPort()); serverSocket.close(); } catch (IOException e) { throw Throwables.propagate(e); } // Setup Zookeeper zkServer = new EmbeddedZookeeper(); String zkConnect = BROKERHOST + ":" + zkServer.port(); zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$); zkUtils = ZkUtils.apply(zkClient, false); // Setup brokers cleanupDir(); Properties props = new Properties(); props.setProperty("zookeeper.connect", zkConnect); props.setProperty("broker.id", "0"); props.setProperty("log.dirs", KAFKA_PATH); props.setProperty("listeners", "PLAINTEXT://" + BROKERHOST + ":" + BROKERPORT); KafkaConfig config = new KafkaConfig(props); Time mock = new MockTime(); kafkaServer = TestUtils.createServer(config, mock); }
From source file:org.apache.oozie.test.EmbeddedServletContainer.java
/** * Start the servlet container. The container starts on a free port. * * @throws Exception thrown if the container could not start. *///from w w w .j a va 2s . c o m public void start() throws Exception { if (StringUtils.isEmpty(host)) { host = InetAddress.getLocalHost().getHostName(); } ServerSocket ss = new ServerSocket(0); if (this.port == 0) { port = ss.getLocalPort(); } ss.close(); server.getConnectors()[0].setHost(host); server.getConnectors()[0].setPort(port); server.start(); System.out.println("Running embedded servlet container at: http://" + host + ":" + port); }
From source file:org.apache.ftpserver.socketfactory.FtpSocketFactoryTest.java
private void testCreateServerSocket(final FtpSocketFactory ftpSocketFactory, final int port) throws Exception, IOException { boolean freePort = false; try {/*from ww w . j av a 2s. c o m*/ final ServerSocket testSocket = new ServerSocket(port, 100); freePort = testSocket.isBound(); testSocket.close(); } catch (Exception exc) { // ok freePort = true; } if (freePort) { new Thread() { public void run() { synchronized (this) { try { this.wait(1000); } catch (InterruptedException e) { e.printStackTrace(); fail(e.toString()); } } try { Socket socket = new Socket(); socket.connect(new InetSocketAddress("localhost", port)); socket.getInputStream(); socket.getOutputStream(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); fail(e.toString()); } catch (IOException e) { e.printStackTrace(); fail(e.toString()); } } }.start(); ServerSocket serverSocket = ftpSocketFactory.createServerSocket(); assertNotNull(serverSocket); serverSocket.accept(); } }
From source file:com.kixeye.kixmpp.client.KixmppClientTest.java
@Before public void setUp() throws Exception { domain = UUID.randomUUID().toString().replace("-", ""); username = UUID.randomUUID().toString().replace("-", ""); password = UUID.randomUUID().toString().replace("-", ""); resource = UUID.randomUUID().toString().replace("-", ""); ServerSocket socketServer = new ServerSocket(); socketServer.bind(null);/*from w w w . ja v a 2s. c o m*/ port = socketServer.getLocalPort(); socketServer.close(); StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry(); final Entity adminJID = EntityImpl.parseUnchecked(username + "@" + domain); final AccountManagement accountManagement = (AccountManagement) providerRegistry .retrieve(AccountManagement.class); if (!accountManagement.verifyAccountExists(adminJID)) { accountManagement.addUser(adminJID, password); } TCPEndpoint tcpEndpoint = new TCPEndpoint(); tcpEndpoint.setPort(port); try (InputStream certStream = this.getClass().getResourceAsStream("/bogus_mina_tls.cert")) { server = new XMPPServer(domain); server.addEndpoint(tcpEndpoint); server.setStorageProviderRegistry(providerRegistry); server.setTLSCertificateInfo(certStream, "boguspw"); server.start(); server.addModule(new SoftwareVersionModule()); server.addModule(new EntityTimeModule()); server.addModule(new XmppPingModule()); server.addModule(new InBandRegistrationModule()); server.addModule(new AdhocCommandsModule()); final ServiceAdministrationModule serviceAdministrationModule = new ServiceAdministrationModule(); // unless admin user account with a secure password is added, this will be not become effective serviceAdministrationModule.setAddAdminJIDs(Arrays.asList(adminJID)); server.addModule(serviceAdministrationModule); } }
From source file:com.kixeye.relax.AsyncRestClientTest.java
@Before public void setUp() throws Exception { Server server = new ContainerServer(testContainer); connection = new SocketConnection(server); ServerSocket socketServer = new ServerSocket(0); port = socketServer.getLocalPort();/*w w w. ja v a 2s . co m*/ socketServer.close(); connection.connect(new InetSocketAddress(port)); }
From source file:com.barchart.netty.server.http.handlers.TestStaticResourceHandler.java
@Before public void setUp() throws Exception { final ServerSocket s = new ServerSocket(0); port = s.getLocalPort();/*from w w w.ja va 2 s . c o m*/ s.close(); server = Servers.createHttpServer().group(new NioEventLoopGroup()) .requestHandler("/classpath", new StaticResourceHandler(this.getClass().getClassLoader(), "/files")) .requestHandler("/file", new StaticResourceHandler( new File(System.getProperty("user.dir") + "/src/test/resources/files"))); server.listen(port, "localhost"); client = new DefaultHttpClient(new PoolingClientConnectionManager()); }
From source file:LCModels.MessageListener.java
public MessageListener(Session s) { int test = 0; try {// w ww . j a v a2s .c om ServerSocket ss1 = new ServerSocket(0); test = ss1.getLocalPort(); ss1.close(); } catch (IOException ex) { Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex); } s.setPort(test); try { server = new ServerSocket(test); } catch (IOException ex) { System.out.println("ERROR KO!"); Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex); } }