List of usage examples for java.net ServerSocket ServerSocket
public ServerSocket(int port) throws IOException
From source file:com.googlecode.jcimd.DummyCimdServer.java
public void start() throws IOException { this.serverSocket = new ServerSocket(this.port); if (this.logger.isInfoEnabled()) { this.logger.info("Listening on port " + this.port); }/*from w ww .j av a2s. co m*/ Runnable listener = new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { try { Socket socket = serverSocket.accept(); socket.setSoTimeout(2000); if (logger.isInfoEnabled()) { logger.info("Starting session with " + socket.getInetAddress().getHostAddress() + ":" + socket.hashCode()); } DummyCimdServer.Session session = new Session(socket); //List<Session> sessions = ...; //sessions.add(session); new Thread(session).start(); } catch (SocketException e) { // Ignore, as this was due to #stop } } } catch (IOException e) { throw new RuntimeException(e); } } }; this.thread = new Thread(listener, this.getClass().getName() + "-listener"); this.thread.start(); }
From source file:io.codis.jodis.TestRoundRobinJedisPool.java
private static int probeFreePort() throws IOException { try (ServerSocket ss = new ServerSocket(0)) { ss.setReuseAddress(true);/*from ww w . j a va 2 s . com*/ return ss.getLocalPort(); } }
From source file:com.cooksys.httpserver.RequestListenerThread.java
public RequestListenerThread(final int port, final HttpService httpService) throws IOException { this.connFactory = DefaultBHttpServerConnectionFactory.INSTANCE; this.serversocket = new ServerSocket(port); this.httpService = httpService; }
From source file:org.glowroot.agent.plugin.netty.NettyIT.java
private static int getAvailablePort() throws Exception { ServerSocket serverSocket = new ServerSocket(0); int port = serverSocket.getLocalPort(); serverSocket.close();//w w w. j ava 2 s. co m return port; }
From source file:org.jasig.cas.adaptors.x509.util.MockWebServer.java
/** * Creates a new server that listens for requests on the given port and * serves the given resource for all requests. * * @param port Server listening port./*from w w w . j a v a2 s .c o m*/ * @param resource Resource to serve. * @param contentType MIME content type of resource to serve. */ public MockWebServer(final int port, final Resource resource, final String contentType) { try { this.worker = new Worker(new ServerSocket(port), resource, contentType); } catch (IOException e) { throw new RuntimeException("Cannot create Web server", e); } }
From source file:cqels_shim.SocketStream.java
/** * start listening on the socket and forwarding to cqels *///from ww w . ja va2 s .c om public void run() { ServerSocket ssock = null; Socket sock = null; try { ssock = new ServerSocket(this.port); sock = ssock.accept(); DataInputStream is = new DataInputStream(sock.getInputStream()); JSONParser parser = new JSONParser(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null && !stop) { try { Object obj = parser.parse(line); JSONArray array = (JSONArray) obj; //stream the triple stream(n((String) array.get(0)), n((String) array.get(1)), n((String) array.get(2))); } catch (ParseException pe) { System.err.println("Error when parsing input, incorrect JSON."); } if (sleep > 0) { try { Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } }
From source file:github.daneren2005.serverproxy.ServerProxy.java
public ServerProxy(Context context) { // Create listening socket try {/*w w w.ja va2 s. com*/ socket = new ServerSocket(0); socket.setSoTimeout(5000); port = socket.getLocalPort(); this.context = context; } catch (UnknownHostException e) { // impossible } catch (IOException e) { Log.e(TAG, "IOException initializing server", e); } }
From source file:info.sugoiapps.xoserver.XOverServer.java
/** * Create new MouseServer./*from www . j av a 2 s . c om*/ * Initiate connection to a listen port and create a robot for mouse imitation. * @param sf JTextField to modify */ public XOverServer(JTextField sf) { //switcher = sw; hostAddress = null; addressReceived = false; statusField = sf; file = null; try { server = new ServerSocket(PORT); } catch (IOException ex) { ex.printStackTrace(); } try { rbt = new Robot(); } catch (AWTException ex) { ex.printStackTrace(); } }
From source file:GossipP2PServer.java
/** * Setup current server as a concurrent server. * @param port Port that server is listening on. *//*ww w.ja v a 2 s . co m*/ static void runConcurrentServer(int port) { Socket sock; try { ServerSocket serverSocket = new ServerSocket(port); DatagramSocket dgSocket = new DatagramSocket(port); for (;;) { new ConcurrentUDPServerThread(dgSocket).start(); sock = serverSocket.accept(); new ConcurrentTCPServerThread(sock).start(); } } catch (Exception e) { } }
From source file:org.apache.solr.prometheus.exporter.SolrExporterTest.java
@Test public void testExecute() throws Exception { // solr client CloudSolrClient cloudSolrClient = cluster.getSolrClient(); int port;//w w w .ja v a 2 s . c o m ServerSocket socket = null; try { socket = new ServerSocket(0); port = socket.getLocalPort(); } finally { socket.close(); } // index sample docs File exampleDocsDir = new File(getFile("exampledocs").getAbsolutePath()); List<File> xmlFiles = Arrays.asList(exampleDocsDir.listFiles((dir, name) -> name.endsWith(".xml"))); for (File xml : xmlFiles) { ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update"); req.addFile(xml, "application/xml"); cloudSolrClient.request(req, "collection1"); } cloudSolrClient.commit("collection1"); // start exporter SolrExporter solrExporter = new SolrExporter(port, cloudSolrClient, getFile("conf/solr-exporter-config.xml").toPath(), 1); try { solrExporter.start(); URI uri = new URI("http://localhost:" + String.valueOf(port) + "/metrics"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; try { HttpGet request = new HttpGet(uri); response = httpclient.execute(request); int expectedHTTPStatusCode = HttpStatus.SC_OK; int actualHTTPStatusCode = response.getStatusLine().getStatusCode(); assertEquals(expectedHTTPStatusCode, actualHTTPStatusCode); } finally { response.close(); httpclient.close(); } } finally { solrExporter.stop(); } }