List of usage examples for java.net ServerSocket accept
public Socket accept() throws IOException
From source file:org.apache.stratos.python.cartridge.agent.test.PythonAgentTestManager.java
/** * Start server socket//from w w w . j a v a 2 s . c o m * * @param port */ protected void startServerSocket(final int port) { Thread socketThread = new Thread(new Runnable() { @Override public void run() { while (true) { // do this infinitely until test is complete try { ServerSocket serverSocket = new ServerSocket(port); serverSocketMap.put(port, serverSocket); log.info("Server socket started on port: " + port); Socket socket = serverSocket.accept(); log.info("Client connected to [port] " + port); InputStream is = socket.getInputStream(); byte[] buffer = new byte[1024]; int read; while (true) { if (socket.isClosed()) { log.info("Socket for [port] " + port + " has been closed."); break; } if ((read = is.read(buffer)) != -1) { String output = new String(buffer, 0, read); log.info("Message received for [port] " + port + ", [message] " + output); } } } catch (IOException e) { String message = "Could not start server socket: [port] " + port; log.error(message, e); throw new RuntimeException(message, e); } } } }); socketThread.start(); }
From source file:com.appeligo.channelfeed.CaptureApp.java
private void catchCaptions(final String captionPort) { Thread serverThread = new Thread("Caption Catcher") { @Override// w ww . j a v a2 s . co m public void run() { try { int count = 0; ServerSocket server = new ServerSocket(Integer.parseInt(captionPort)); while (true) { try { count++; VBISocketReaderThread vbiSocketReaderThread = new VBISocketReaderThread( "VBISocketReader " + count + "@" + captionPort, server.accept()); Destinations destinations = new Destinations(); destinations.setCaptionDocumentRoot(captionDocumentRoot); destinations.setDestinationURLs(destinationURLs, destinationRaws); destinations.setSendXDS(false); destinations.setSendITV(false); if (writing) { destinations.setFileWriter(new FileWriter()); } vbiSocketReaderThread.setEpgService(getEpgService()); vbiSocketReaderThread.setDestinations(destinations); destinations.connect(); vbiSocketReaderThread.start(); } catch (MalformedURLException e1) { log.error("Exception on a channel", e1); } catch (IOException e1) { log.error("Exception on a channel", e1); } } } catch (IOException e1) { log.error("Exception with server socket", e1); } catch (NumberFormatException e1) { log.error("Bad port number: " + captionPort, e1); return; } } }; serverThread.start(); }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceSimpleOutput() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;//from w ww .j a v a 2 s . c o m try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 0); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check"); runner.start(); channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("test1\n"); writer.write("check\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.interrupt(); runner.waitUntilDone(); channel.close(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.echocat.jomon.net.dns.DnsServer.java
@Nonnull private Socket accept(@Nonnull ServerSocket sock) throws IOException { try {// ww w . j av a 2 s . c om return sock.accept(); } catch (final SocketException e) { if (sock.isClosed()) { final InterruptedIOException toThrow = new InterruptedIOException(); toThrow.initCause(e); throw toThrow; } else { throw e; } } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceOutputWithRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/*from w w w . ja v a 2 s. c o m*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check"); runner.start(); // first connection: nothing channel = server.accept(); channel.close(); // second connection: first string channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("test1\n"); writer.close(); channel.close(); // third connection: nothing channel = server.accept(); channel.close(); // forth connection: second string channel = server.accept(); writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("check\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.waitUntilDone(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceOutputInfiniteRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/* ww w .j a v a 2 s . co m*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", -1, 100); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check"); runner.start(); // first connection: nothing channel = server.accept(); channel.close(); // second connection: first string channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("test1\n"); writer.close(); channel.close(); // third connection: nothing channel = server.accept(); channel.close(); // forth connection: second string channel = server.accept(); writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("check\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.waitUntilDone(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceOutputAcrossRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/*from ww w. j a v a 2 s . co m*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check1", "check2"); runner.start(); // first connection: nothing channel = server.accept(); channel.close(); // second connection: first string channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("te"); writer.close(); channel.close(); // third connection: nothing channel = server.accept(); channel.close(); // forth connection: second string channel = server.accept(); writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("st1\n"); writer.write("check1\n"); writer.write("check2\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.waitUntilDone(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:com.apporiented.hermesftp.server.AbstractFtpServer.java
/** * {@inheritDoc}/* w ww . ja v a 2 s .c o m*/ */ public void run() { setStatus(SERVER_STATUS_INIT); ServerSocket serverSocket = null; try { getUserManager().load(); serverSocket = createServerSocket(); serverSocket.setSoTimeout(DEFAULT_TIMEOUT); setStatus(SERVER_STATUS_READY); while (!isTerminated()) { Socket clientSocket; try { clientSocket = serverSocket.accept(); } catch (SocketTimeoutException e) { continue; } /* Check blacklisted IP v4 addresses */ InetAddress clientAddr = clientSocket.getInetAddress(); InetAddress localAddr = clientSocket.getLocalAddress(); log.info("Client requests connection. ClientAddr.: " + clientAddr + ", LocalAddr.: " + localAddr); String listKey = NetUtils.isIPv6(clientAddr) ? FtpConstants.OPT_IPV6_BLACK_LIST : FtpConstants.OPT_IPV4_BLACK_LIST; String ipBlackList = getOptions().getString(listKey, ""); if (NetUtils.checkIPMatch(ipBlackList, clientAddr)) { log.info("Client with IP address " + clientAddr.getHostAddress() + " rejected (blacklisted)."); IOUtils.closeGracefully(clientSocket); continue; } /* Initialize session context */ FtpSessionContext ctx = createFtpContext(); ctx.check(); ctx.setCreationTime(new Date()); ctx.setClientSocket(clientSocket); FtpSession session = (FtpSession) getApplicationContext().getBean(BEAN_SESSION); session.setFtpContext(ctx); /* Start session */ log.debug("Accepting connection to " + clientAddr.getHostAddress()); session.start(); registerSession(session); } setStatus(SERVER_STATUS_HALTED); } catch (IOException e) { setStatus(SERVER_STATUS_UNDEF); log.error(e, e); } finally { terminateAllClientSessions(); IOUtils.closeGracefully(serverSocket); } }
From source file:org.y20k.transistor.helpers.MetadataHelper.java
private void createShoutcastProxyConnection() { closeShoutcastProxyConnection();//from w w w.j av a 2s. c om mProxyRunning = true; final StringBuffer shoutcastProxyUri = new StringBuffer(); try { new Thread(new Runnable() { @Override public void run() { Socket proxy = null; URLConnection connection = null; try { final ServerSocket proxyServer = new ServerSocket(0, 1, InetAddress.getLocalHost()); shoutcastProxyUri.append("http://localhost:") .append(String.valueOf(proxyServer.getLocalPort())).append("/"); LogHelper.v(LOG_TAG, "createProxyConnection: " + shoutcastProxyUri.toString()); proxy = proxyServer.accept(); mProxyConnection = proxy; proxyServer.close(); connection = new URL(mStreamUri).openConnection(); shoutcastProxyReaderLoop(proxy, connection); } catch (Exception e) { LogHelper.e(LOG_TAG, "Error: Unable to create proxy server. (" + e + ")"); } mProxyRunning = false; try { if (connection != null) { ((HttpURLConnection) connection).disconnect(); } } catch (Exception ee) { LogHelper.e(LOG_TAG, "Error: Unable to disconnect HttpURLConnection. (" + ee + ")"); } try { if (proxy != null && !proxy.isClosed()) { proxy.close(); } } catch (Exception eee) { LogHelper.e(LOG_TAG, "Error: Unable to close proxy. (" + eee + ")"); } } }).start(); while (shoutcastProxyUri.length() == 0) { try { Thread.sleep(10); } catch (Exception e) { LogHelper.e(LOG_TAG, "Error: Unable to Thread.sleep. (" + e + ")"); } } mShoutcastProxy = shoutcastProxyUri.toString(); } catch (Exception e) { LogHelper.e(LOG_TAG, "createProxyConnection: Cannot create new listening socket on localhost: " + e.toString()); mProxyRunning = false; mShoutcastProxy = ""; } }
From source file:TalkServerThread.java
protected TalkServerThread connectToClient(ServerSocket serverRSocket) { Socket rendezvousSocket = null; TalkServerThread tst = null;//from ww w .j a v a 2s .co m //Listen for client connection on the rendezvous socket. try { rendezvousSocket = serverRSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); e.printStackTrace(); return null; } //Create a thread to handle this connection. try { tst = new TalkServerThread(rendezvousSocket, this); tst.start(); } catch (Exception e) { System.err.println("Couldn't create TalkServerThread:"); e.printStackTrace(); return null; } writeToStream("Successful connection. " + "Please wait for second applet to connect...", tst.os); return tst; }