List of usage examples for java.net ServerSocket accept
public Socket accept() throws IOException
From source file:org.apache.htrace.impl.HTracedProcess.java
private static StartupNotificationData readStartupNotification(ServerSocket listener) throws IOException { Socket socket = listener.accept(); try {//w w w . j a v a 2 s . c o m InputStream in = socket.getInputStream(); ObjectMapper objectMapper = new ObjectMapper(); StartupNotificationData data = objectMapper.readValue(in, StartupNotificationData.class); return data; } finally { socket.close(); } }
From source file:pt.minha.calibration.Calibrator.java
private static void runServer() throws Exception { // Reality server ServerSocket ss = new ServerSocket(12345); logger.info("server: started at {}", ss.getLocalSocketAddress()); while (true) { Socket s = ss.accept(); logger.info("server: accepted {}", s.getRemoteSocketAddress()); try {//from w ww . j a v a 2s . c o m ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream())); oos.flush(); ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(s.getInputStream())); while (!s.isClosed()) { Map<String, Object> p = (Map<String, Object>) ois.readObject(); Benchmark next = (Benchmark) Class.forName((String) p.get("bench")).newInstance(); next.setParameters(p); logger.info("server: running {}", p); Object result = next.server(); logger.info("server: running {} done", p); oos.writeObject(result); oos.flush(); } logger.info("server: disconnected {}", s.getRemoteSocketAddress()); } catch (IOException ioe) { logger.info("server: disconnected {} on {}", s.getRemoteSocketAddress(), ioe); } } }
From source file:org.eclipse.wst.xsl.jaxp.debug.debugger.DebugRunner.java
private static Socket getSocket(int port) throws IOException { InetAddress localhost = InetAddress.getByName("localhost"); //$NON-NLS-1$ ServerSocket serverSocket = new ServerSocket(port, 5, localhost); Socket clientSocket = serverSocket.accept(); serverSocket.close();/*w w w .j a va 2 s.c o m*/ return clientSocket; }
From source file:SimpleProxyServer.java
/** * runs a single-threaded proxy server on * the specified local port. It never returns. */// w w w.j a v a2s .c om public static void runServer(String host, int remoteport, int localport) throws IOException { // Create a ServerSocket to listen for connections with ServerSocket ss = new ServerSocket(localport); final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; while (true) { Socket client = null, server = null; try { // Wait for a connection on the local port client = ss.accept(); final InputStream streamFromClient = client.getInputStream(); final OutputStream streamToClient = client.getOutputStream(); // Make a connection to the real server. // If we cannot connect to the server, send an error to the // client, disconnect, and continue waiting for connections. try { server = new Socket(host, remoteport); } catch (IOException e) { PrintWriter out = new PrintWriter(streamToClient); out.print("Proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e + "\n"); out.flush(); client.close(); continue; } // Get server streams. final InputStream streamFromServer = server.getInputStream(); final OutputStream streamToServer = server.getOutputStream(); // a thread to read the client's requests and pass them // to the server. A separate thread for asynchronous. Thread t = new Thread() { public void run() { int bytesRead; try { while ((bytesRead = streamFromClient.read(request)) != -1) { streamToServer.write(request, 0, bytesRead); streamToServer.flush(); } } catch (IOException e) { } // the client closed the connection to us, so close our // connection to the server. try { streamToServer.close(); } catch (IOException e) { } } }; // Start the client-to-server request thread running t.start(); // Read the server's responses // and pass them back to the client. int bytesRead; try { while ((bytesRead = streamFromServer.read(reply)) != -1) { streamToClient.write(reply, 0, bytesRead); streamToClient.flush(); } } catch (IOException e) { } // The server closed its connection to us, so we close our // connection to our client. streamToClient.close(); } catch (IOException e) { System.err.println(e); } finally { try { if (server != null) server.close(); if (client != null) client.close(); } catch (IOException e) { } } } }
From source file:ExecutorHttpd.java
public void start(int port) throws IOException { final ServerSocket ss = new ServerSocket(port); while (!executor.isShutdown()) executor.submit(new TinyHttpdConnection(ss.accept())); }
From source file:org.hoshi.playground.net.sockets.CapitalizeServer.java
private void start() throws IOException { int clientNumber = 0; final ServerSocket listener = new ServerSocket(PORT_TO_LISTEN); try {/* w w w . j ava 2 s .c om*/ while (true) { new Thread(new Capitalizer(listener.accept(), clientNumber++)).start(); } } finally { IOUtils.closeQuietly(listener); } }
From source file:org.huahinframework.emanager.ShutDowner.java
@Override public void run() { log.info("ShutDowner start"); try {/*from w w w. ja v a 2s.c om*/ ServerSocket svsock = new ServerSocket(port); for (;;) { Socket socket = svsock.accept(); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = reader.readLine(); reader.close(); socket.close(); if (line.matches(OPERATION_STOP)) { server.stop(); queueManager.terminate(); break; } } svsock.close(); } catch (Exception e) { log.error(e); e.printStackTrace(); } log.info("ShutDowner end"); }
From source file:org.mule.transport.http.functional.MockHttpServer.java
public void run() { try {/*from w ww . java 2s. c o m*/ ServerSocket serverSocket = new ServerSocket(listenPort); startupLatch.countDown(); Socket clientSocket = serverSocket.accept(); InputStream in = clientSocket.getInputStream(); OutputStream out = clientSocket.getOutputStream(); processRequests(in, out); in.close(); out.close(); clientSocket.close(); serverSocket.close(); shutdownLatch.countDown(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:net.usikkert.kouchat.testclient.TestClientTelnetServer.java
private void startServer() { processMessages = true;/*from w w w .java2 s . c o m*/ try { final ServerSocket serverSocket = new ServerSocket(20000); socket = serverSocket.accept(); IOUtils.closeQuietly(serverSocket); writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); ui.setWriter(writer); processMessages(); } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:to.sven.androidrccar.host.communication.impl.SocketConnector.java
/** * Start to listen on the {@link ServerSocket}. *//* w w w. jav a2 s . c o m*/ @Override protected AsyncTaskResult<Socket> doInBackground(Object... params) { try { ServerSocket serverSocket = new ServerSocket(port); Socket socket = serverSocket.accept(); serverSocket.close(); return new AsyncTaskResult<Socket>(socket); } catch (Exception ex) { return new AsyncTaskResult<Socket>(ex); } }