List of usage examples for java.net ServerSocket ServerSocket
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
From source file:com.irccloud.android.GingerbreadImageProxy.java
public void init() { try {// w ww .ja v a 2s . com socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })); socket.setSoTimeout(5000); port = socket.getLocalPort(); } catch (UnknownHostException e) { Log.e(LOG_TAG, "Error initializing server", e); } catch (IOException e) { Log.e(LOG_TAG, "Error initializing server", e); } }
From source file:com.machinepublishers.jbrowserdriver.diagnostics.HttpServer.java
public static void launch(int port) { if (loop.compareAndSet(false, true)) { new Thread(new Runnable() { @Override/*from w ww .jav a 2s. c o m*/ public void run() { try (ServerSocket serverSocket = new ServerSocket(port, 50, InetAddress.getLoopbackAddress())) { listener.set(serverSocket); while (loop.get()) { try (Socket socket = serverSocket.accept(); DataOutputStream output = new DataOutputStream(socket.getOutputStream()); BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream()))) { List<String> request = new ArrayList<String>(); for (String line; (line = reader.readLine()) != null;) { request.add(line); if (line.startsWith("GET / ")) { output.write(indexContent, 0, indexContent.length); output.write(indexBody, 0, indexBody.length); } else if (line.startsWith("POST / ")) { output.write(postContent, 0, postContent.length); output.write(postBody, 0, postBody.length); } else if (line.startsWith("GET /iframe.htm")) { output.write(iframeContent, 0, iframeContent.length); output.write(iframeBody, 0, iframeBody.length); } else if (line.startsWith("GET /redirect/site1 ")) { output.write(redirectContent); } else if (line.startsWith("GET /redirect/site2 ")) { output.write(iframeContent, 0, iframeContent.length); output.write(iframeBody, 0, iframeBody.length); } else if (line.startsWith("GET /wait-forever ")) { synchronized (HttpServer.class) { HttpServer.class.wait(); } } else if (line.startsWith("GET /image.png")) { output.write(imageContent, 0, imageContent.length); output.write(imageBody, 0, imageBody.length); } } previousRequest.set(request); previousRequestId.incrementAndGet(); } } } catch (Throwable t) { } } }).start(); } }
From source file:org.apache.axis2.soapmonitor.servlet.SOAPMonitorService.java
/** * Servlet initialiation//from w w w .j a va 2s . c o m */ public void init() throws ServletException { if (connections == null) { // Create vector to hold connection information connections = new Vector(); } if (serverSocket == null) { // Get the server socket port from the init params ServletConfig config = super.getServletConfig(); String hostName = config.getInitParameter(SOAPMonitorConstants.SOAP_MONITOR_HOST_NAME); String port = config.getInitParameter(SOAPMonitorConstants.SOAP_MONITOR_PORT); if (port == null) { log.error("SOAPMonitorService can't find ServletConfig init parameter 'port'"); port = "0"; } try { if (hostName != null) { serverSocket = new ServerSocket(Integer.parseInt(port), 50, InetAddress.getByName(hostName)); } else { serverSocket = new ServerSocket(Integer.parseInt(port)); } } catch (Exception ex) { // Let someone know we could not open the socket log.error("Unable to open server socket using port: " + port); log.error(ex.getMessage(), ex); // Fail as loudly as possible for those without logging configured config.getServletContext().log("Unable to open server socket using port " + port + ":", ex); serverSocket = null; } if (serverSocket != null) { // Start the server socket thread new Thread(new ServerSocketThread()).start(); } } }
From source file:org.qi4j.library.shiro.AbstractServletTestSupport.java
protected static int findFreePortOnIfaceWithPreference(final InetAddress address, final int prefered) throws IOException { ServerSocket server;/* www.ja v a2 s. c o m*/ if (prefered > 0) { server = new ServerSocket(prefered, 1, address); } else { server = new ServerSocket(0, 1, address); } int port = server.getLocalPort(); server.close(); return port; }
From source file:org.muckebox.android.net.DownloadServer.java
@Override public void run() { super.run();//from w ww.j a va 2 s . co m try { d("Starting server on " + mPort); mServerSocket = new ServerSocket(mPort, 0, InetAddress.getByName("localhost")); mServerSocket.setReuseAddress(true); try { mReady = true; while (!mStopped) { Socket socket = null; DefaultHttpServerConnection connection = new DefaultHttpServerConnection(); try { d("Waiting for new connection"); socket = mServerSocket.accept(); d("Got connection"); connection.bind(socket, new BasicHttpParams()); mHttpService.handleRequest(connection, mHttpContext); d("Request handling finished"); } catch (HttpException e) { Log.e(LOG_TAG, "Got HTTP error: " + e); } finally { connection.shutdown(); if (socket != null) socket.close(); socket = null; } } } finally { if (mServerSocket != null) mServerSocket.close(); } d("Server stopped"); } catch (IOException e) { Log.i(LOG_TAG, "IOException, probably ok"); e.printStackTrace(); } }
From source file:com.techcavern.pircbotz.IdentServer.java
/** * Create an ident server on port 113 with the specified encoding * * @param encoding Encoding to use for sockets *///from w w w . j av a 2 s. c o m protected IdentServer(Charset encoding, InetAddress localAddress, int port) { try { this.encoding = encoding; this.localAddress = localAddress; this.serverSocket = new ServerSocket(port, 50, localAddress); this.port = port; } catch (Exception e) { throw new RuntimeException("Could not create server socket for IdentServer on " + localAddress.toString() + ", port " + port, e); } }
From source file:com.shonshampain.streamrecorder.util.StreamProxy.java
private void init() { try {/*from w w w . jav a 2 s .c o m*/ socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })); socket.setSoTimeout(5000); port = socket.getLocalPort(); Logger.d(DBG, TAG, "port " + port + " obtained"); } catch (IOException e) { Logger.e(TAG, "Error initializing server", e); } }
From source file:com.zotoh.maedr.device.TcpIO.java
private ServerSocket createSvrSockIt() throws IOException { InetAddress ip;/*from w w w . j ava 2 s. co m*/ ip = isEmpty(_host) ? InetAddress.getLocalHost() : InetAddress.getByName(_host); ServerSocket soc = new ServerSocket(_port, _backlog, ip); ServerSocket s = null; try { soc.setReuseAddress(true); s = soc; soc = null; } finally { NetUte.close(soc); } tlog().debug("TCP: opened server socket: {} on host: {}", _port, _host); return s; }
From source file:com.sshtools.daemon.SshServer.java
/** * * * @throws IOException/*from ww w .j a va 2 s. c om*/ */ protected void startCommandSocket() throws IOException { try { /** * A potential problem exists here. What happens if the command * server socket is dropped for any reason? This will cause the * server to stop. */ commandServerSocket = new ServerSocket( ((ServerConfiguration) ConfigurationLoader.getConfiguration(ServerConfiguration.class)) .getCommandPort(), 50, InetAddress.getByName("127.0.0.1")); Socket client; while ((client = commandServerSocket.accept()) != null) { log.info("Command request received"); // Read and process the command processCommand(client.getInputStream().read(), client); client.close(); if (shutdown) { break; } } commandServerSocket.close(); } catch (Exception e) { if (!shutdown) { log.fatal("The command socket failed", e); } } }
From source file:com.sshtools.j2ssh.forwarding.ForwardingListener.java
/** * * * @throws IOException/*from w w w . j ava 2 s .co m*/ */ public void start() throws IOException { /* Set the state by calling the super method */ super.start(); /* Bind server socket */ try { server = new ServerSocket(getPortToBind(), 50, InetAddress.getByName(getAddressToBind())); } catch (IOException ioe) { super.stop(); throw ioe; } /* Create a thread and start it */ thread = new SshThread(this, "Forwarding listener", true); /* Create a thread and start it */ thread = new SshThread(this, "Forwarding listener", true); thread.start(); }