List of usage examples for java.net ServerSocket ServerSocket
public ServerSocket(int port) throws IOException
From source file:MiniCluster.java
/** * Runs the {@link MiniAccumuloCluster} given a -p argument with a property file. Establishes a shutdown port for asynchronous operation. * //w ww. j av a 2 s . com * @param args * An optional -p argument can be specified with the path to a valid properties file. */ public static void main(String[] args) throws Exception { Opts opts = new Opts(); opts.parseArgs(MiniCluster.class.getName(), args); if (opts.printProps) { printProperties(); System.exit(0); } int shutdownPort = 4445; final File miniDir; if (opts.prop.containsKey(DIRECTORY_PROP)) miniDir = new File(opts.prop.getProperty(DIRECTORY_PROP)); else miniDir = Files.createTempDir(); String rootPass = opts.prop.containsKey(ROOT_PASSWORD_PROP) ? opts.prop.getProperty(ROOT_PASSWORD_PROP) : "secret"; String instanceName = opts.prop.containsKey(INSTANCE_NAME_PROP) ? opts.prop.getProperty(INSTANCE_NAME_PROP) : "accumulo"; MiniAccumuloConfig config = new MiniAccumuloConfig(miniDir, instanceName, rootPass); if (opts.prop.containsKey(NUM_T_SERVERS_PROP)) config.setNumTservers(Integer.parseInt(opts.prop.getProperty(NUM_T_SERVERS_PROP))); if (opts.prop.containsKey(ZOO_KEEPER_PORT_PROP)) config.setZooKeeperPort(Integer.parseInt(opts.prop.getProperty(ZOO_KEEPER_PORT_PROP))); // if (opts.prop.containsKey(JDWP_ENABLED_PROP)) // config.setJDWPEnabled(Boolean.parseBoolean(opts.prop.getProperty(JDWP_ENABLED_PROP))); // if (opts.prop.containsKey(ZOO_KEEPER_MEMORY_PROP)) // setMemoryOnConfig(config, opts.prop.getProperty(ZOO_KEEPER_MEMORY_PROP), ServerType.ZOOKEEPER); // if (opts.prop.containsKey(TSERVER_MEMORY_PROP)) // setMemoryOnConfig(config, opts.prop.getProperty(TSERVER_MEMORY_PROP), ServerType.TABLET_SERVER); // if (opts.prop.containsKey(MASTER_MEMORY_PROP)) // setMemoryOnConfig(config, opts.prop.getProperty(MASTER_MEMORY_PROP), ServerType.MASTER); // if (opts.prop.containsKey(DEFAULT_MEMORY_PROP)) // setMemoryOnConfig(config, opts.prop.getProperty(DEFAULT_MEMORY_PROP)); // if (opts.prop.containsKey(SHUTDOWN_PORT_PROP)) // shutdownPort = Integer.parseInt(opts.prop.getProperty(SHUTDOWN_PORT_PROP)); Map<String, String> siteConfig = new HashMap<String, String>(); for (Map.Entry<Object, Object> entry : opts.prop.entrySet()) { String key = (String) entry.getKey(); if (key.startsWith("site.")) siteConfig.put(key.replaceFirst("site.", ""), (String) entry.getValue()); } config.setSiteConfig(siteConfig); final MiniAccumuloCluster accumulo = new MiniAccumuloCluster(config); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { accumulo.stop(); FileUtils.deleteDirectory(miniDir); System.out.println("\nShut down gracefully on " + new Date()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); accumulo.start(); printInfo(accumulo, shutdownPort); // start a socket on the shutdown port and block- anything connected to this port will activate the shutdown ServerSocket shutdownServer = new ServerSocket(shutdownPort); shutdownServer.accept(); System.exit(0); }
From source file:Main.java
public static int findFreePort() throws IOException { try (ServerSocket serverSocket = new ServerSocket(0)) { return serverSocket.getLocalPort(); }/*from w w w . j av a2s. com*/ }
From source file:Main.java
public static boolean portIsUsing(int port) { ServerSocket serverSocket = null; try {/* ww w . j a va 2 s . c om*/ serverSocket = new ServerSocket(port); } catch (IOException ex) { return true; } try { serverSocket.close(); } catch (IOException ex) { } return false; }
From source file:LocalScan.java
private static void testPort(int i) { try {//from www . j a v a 2 s .co m ServerSocket sock = new ServerSocket(i); } catch (java.io.IOException e) { System.out.println("Port " + i + " in use."); } }
From source file:org.apache.accumulo.minicluster.MiniAccumuloRunner.java
/** * Runs the {@link MiniAccumuloCluster} given a -p argument with a property file. Establishes a shutdown port for asynchronous operation. * * @param args// www.j a v a2 s .com * An optional -p argument can be specified with the path to a valid properties file. */ public static void main(String[] args) throws IOException, InterruptedException { Opts opts = new Opts(); opts.parseArgs(MiniAccumuloRunner.class.getName(), args); if (opts.printProps) { printProperties(); System.exit(0); } int shutdownPort = 4445; final File miniDir; if (opts.prop.containsKey(DIRECTORY_PROP)) miniDir = new File(opts.prop.getProperty(DIRECTORY_PROP)); else miniDir = Files.createTempDir(); String rootPass = opts.prop.containsKey(ROOT_PASSWORD_PROP) ? opts.prop.getProperty(ROOT_PASSWORD_PROP) : "secret"; MiniAccumuloConfig config = new MiniAccumuloConfig(miniDir, rootPass); if (opts.prop.containsKey(INSTANCE_NAME_PROP)) config.setInstanceName(opts.prop.getProperty(INSTANCE_NAME_PROP)); if (opts.prop.containsKey(NUM_T_SERVERS_PROP)) config.setNumTservers(Integer.parseInt(opts.prop.getProperty(NUM_T_SERVERS_PROP))); if (opts.prop.containsKey(ZOO_KEEPER_PORT_PROP)) config.setZooKeeperPort(Integer.parseInt(opts.prop.getProperty(ZOO_KEEPER_PORT_PROP))); if (opts.prop.containsKey(ZOO_KEEPER_STARTUP_TIME_PROP)) config.setZooKeeperStartupTime(Long.parseLong(opts.prop.getProperty(ZOO_KEEPER_STARTUP_TIME_PROP))); if (opts.prop.containsKey(EXISTING_ZOO_KEEPERS_PROP)) config.getImpl().setExistingZooKeepers(opts.prop.getProperty(EXISTING_ZOO_KEEPERS_PROP)); if (opts.prop.containsKey(JDWP_ENABLED_PROP)) config.setJDWPEnabled(Boolean.parseBoolean(opts.prop.getProperty(JDWP_ENABLED_PROP))); if (opts.prop.containsKey(ZOO_KEEPER_MEMORY_PROP)) setMemoryOnConfig(config, opts.prop.getProperty(ZOO_KEEPER_MEMORY_PROP), ServerType.ZOOKEEPER); if (opts.prop.containsKey(TSERVER_MEMORY_PROP)) setMemoryOnConfig(config, opts.prop.getProperty(TSERVER_MEMORY_PROP), ServerType.TABLET_SERVER); if (opts.prop.containsKey(MASTER_MEMORY_PROP)) setMemoryOnConfig(config, opts.prop.getProperty(MASTER_MEMORY_PROP), ServerType.MASTER); if (opts.prop.containsKey(DEFAULT_MEMORY_PROP)) setMemoryOnConfig(config, opts.prop.getProperty(DEFAULT_MEMORY_PROP)); if (opts.prop.containsKey(SHUTDOWN_PORT_PROP)) shutdownPort = Integer.parseInt(opts.prop.getProperty(SHUTDOWN_PORT_PROP)); Map<String, String> siteConfig = new HashMap<String, String>(); for (Map.Entry<Object, Object> entry : opts.prop.entrySet()) { String key = (String) entry.getKey(); if (key.startsWith("site.")) siteConfig.put(key.replaceFirst("site.", ""), (String) entry.getValue()); } config.setSiteConfig(siteConfig); final MiniAccumuloCluster accumulo = new MiniAccumuloCluster(config); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { accumulo.stop(); } catch (IOException e) { log.error("IOException attempting to stop Accumulo.", e); return; } catch (InterruptedException e) { log.error("InterruptedException attempting to stop Accumulo.", e); return; } try { FileUtils.deleteDirectory(miniDir); } catch (IOException e) { log.error("IOException attempting to clean up miniDir.", e); return; } System.out.println("\nShut down gracefully on " + new Date()); } }); accumulo.start(); printInfo(accumulo, shutdownPort); // start a socket on the shutdown port and block- anything connected to this port will activate the shutdown try (ServerSocket shutdownServer = new ServerSocket(shutdownPort)) { shutdownServer.accept().close(); } System.exit(0); }
From source file:SimpleProxyServer.java
/** * runs a single-threaded proxy server on * the specified local port. It never returns. *//*from w w w.j a va 2s. c o m*/ 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:org.carrot2.util.httpclient.HttpClientFactoryTest.java
@BeforeClass public static void setup() throws Exception { serverSocket = new ServerSocket(/* any */0); pseudoServer = new Thread() { public void run() { try { Socket socket;/*w w w .j a va 2s . c o m*/ while ((socket = serverSocket.accept()) != null) { sockets.add(socket); } } catch (SocketException e) { // Ignore, socket closed. } catch (IOException e) { throw new RuntimeException(); } } }; }
From source file:Main.java
public Main() throws IOException { serverSocket = new ServerSocket(8008); serverSocket.setSoTimeout(10000); }
From source file:Main.java
public Main() throws IOException { serverSocket = new ServerSocket(8008); serverSocket.setSoTimeout(10000);/* w w w . jav a 2s .c o m*/ System.out.println(serverSocket.getInetAddress()); }
From source file:Main.java
public Main() throws IOException { serverSocket = new ServerSocket(8008); serverSocket.setSoTimeout(10000); System.out.println(serverSocket.isClosed()); }