List of usage examples for java.net ServerSocket ServerSocket
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
From source file:com.adito.agent.client.tunneling.LocalTunnelServer.java
/** * Start listening for incoming connections to this listener. When * successful, this method will return immediately. // w w w .j a va2 s. c o m * @throws IOException */ public void start() throws IOException { if (stopping) { throw new IOException("Local forwarding is currently stopping."); } if (isListening()) { throw new IOException("Local forwarding is already listening."); } dataLastTransferred = System.currentTimeMillis(); // #ifdef DEBUG if (listeningSocketConfiguration.isPermanent()) { log.info("Starting permanent listening socket on port " + listeningSocketConfiguration.getSourcePort()); //$NON-NLS-1$ } else { log.info("Starting temporary listening socket on port " + listeningSocketConfiguration.getSourcePort()); //$NON-NLS-1$ } // #endif /* Bind server socket */ if (listeningSocketConfiguration.getTransport().equals(TunnelConfiguration.UDP_TUNNEL)) { // #ifdef DEBUG log.info("Creating UDP server socket on port " + listeningSocketConfiguration.getSourcePort()); //$NON-NLS-1$ // #endif datagramSocket = new DatagramSocket(listeningSocketConfiguration.getSourcePort()); } else { // #ifdef DEBUG if (listeningSocketConfiguration.getSourcePort() == 0) log.info("Creating TCP server socket random port"); //$NON-NLS-1$ else log.info("Creating TCP server socket on port " + listeningSocketConfiguration.getSourcePort()); //$NON-NLS-1$ // #endif /* If the specified port is 0 then ServerSocket will select the * next free port. We then need to store the port actually used * back into the configuration so application launching can * work. */ boolean resetPort = listeningSocketConfiguration.getSourcePort() == 0; server = new ServerSocket(listeningSocketConfiguration.getSourcePort(), 50, InetAddress.getByName(getAddressToBind())); if (resetPort) { // #ifdef DEBUG log.info("Chosen port " + server.getLocalPort()); //$NON-NLS-1$ // #endif listeningSocketConfiguration.setSourcePort(server.getLocalPort()); } } fireLocalTunnelServerStarted(); thread = new Thread(new Runnable() { public void run() { tunnelTCP(); } }); thread.setDaemon(true); thread.setName("SocketListener " + getAddressToBind() + ":" //$NON-NLS-1$//$NON-NLS-2$ + String.valueOf(listeningSocketConfiguration.getSourcePort())); thread.start(); }
From source file:edu.vt.middleware.gator.log4j.SocketServer.java
/** * Start the server and listen for incoming connections. * * @throws IOException On failure to bind to desired port. *//* ww w. ja v a 2 s . co m*/ public void start() throws IOException { final String listenIP = inetBindAddress.getHostAddress(); logger.info("Starting SocketServer..."); logger.info(String.format("Listening on %s:%s", listenIP, port)); serverSocket = new ServerSocket(port, 0, inetBindAddress); socketServerThread = new Thread(this, String.format("gator-server-%s-%s", listenIP, port)); handlerExecutor = new LoggingEventHandlerExecutor(); socketServerThread.start(); logger.info("Socket server started successfully."); }
From source file:com.twosigma.beaker.core.Main.java
private static boolean portAvailable(int port) { ServerSocket ss = null;/*from w w w. j a v a2 s . c o m*/ try { InetAddress address = InetAddress.getByName("127.0.0.1"); ss = new ServerSocket(port, 1, address); ss.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; }
From source file:catalina.core.StandardServer.java
/** * Wait until a proper shutdown command is received, then return. *///from w ww. j a va2 s . c o m public void await() { // Set up a server socket to wait on ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException e) { System.err.println("StandardServer.await: create[" + port + "]: " + e); e.printStackTrace(); System.exit(1); } // Loop waiting for a connection and a valid command while (true) { // Wait for the next connection Socket socket = null; InputStream stream = null; try { socket = serverSocket.accept(); socket.setSoTimeout(10 * 1000); // Ten seconds stream = socket.getInputStream(); } catch (AccessControlException ace) { System.err.println("StandardServer.accept security exception: " + ace.getMessage()); continue; } catch (IOException e) { System.err.println("StandardServer.await: accept: " + e); e.printStackTrace(); System.exit(1); } // Read a set of characters from the socket StringBuffer command = new StringBuffer(); int expected = 1024; // Cut off to avoid DoS attack while (expected < shutdown.length()) { if (random == null) random = new Random(System.currentTimeMillis()); expected += (random.nextInt() % 1024); } while (expected > 0) { int ch = -1; try { ch = stream.read(); } catch (IOException e) { System.err.println("StandardServer.await: read: " + e); e.printStackTrace(); ch = -1; } if (ch < 32) // Control character or EOF terminates loop break; command.append((char) ch); expected--; } // Close the socket now that we are done with it try { socket.close(); } catch (IOException e) { ; } // Match against our command string boolean match = command.toString().equals(shutdown); if (match) { break; } else System.err.println("StandardServer.await: Invalid command '" + command.toString() + "' received"); } // Close the server socket and return try { serverSocket.close(); } catch (IOException e) { ; } }
From source file:nl.nn.adapterframework.http.AuthSSLProtocolSocketFactoryBase.java
public ServerSocket createServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { return new ServerSocket(port, backlog, bindAddr); }
From source file:edu.stanford.epadd.launcher.Main.java
public JettyShutdownThread(Server server, int shutdownPort) { this.jettyServer = server; this.shutdownPort = shutdownPort; setDaemon(true);/* w w w. j a v a 2 s.c o m*/ setName("Stop Jetty"); try { socket = new ServerSocket(this.shutdownPort, 1, InetAddress.getByName("127.0.0.1")); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.twosigma.beaker.core.rest.PluginServiceLocatorRest.java
private static boolean isPortAvailable(int port) { ServerSocket ss = null;//from ww w . j a va 2 s .co m try { InetAddress address = InetAddress.getByName("127.0.0.1"); ss = new ServerSocket(port, 1, address); // ss = new ServerSocket(port); ss.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; }