Search in sources :

Example 31 with ServerSocket

use of java.net.ServerSocket in project h2o-2 by h2oai.

the class NanoHTTPD method main.

/**
   * Starts as a standalone file server and waits for Enter.
   */
public static void main(String[] args) {
    myOut.println("NanoHTTPD 1.25 (C) 2001,2005-2011 Jarno Elonen and (C) 2010 Konstantinos Togias\n" + "(Command line options: [-p port] [-d root-dir] [--licence])\n");
    // Defaults
    int port = 80;
    File wwwroot = new File(".").getAbsoluteFile();
    // Show licence if requested
    for (int i = 0; i < args.length; ++i) if (args[i].equalsIgnoreCase("-p"))
        port = Integer.parseInt(args[i + 1]);
    else if (args[i].equalsIgnoreCase("-d"))
        wwwroot = new File(args[i + 1]).getAbsoluteFile();
    else if (args[i].toLowerCase().endsWith("licence")) {
        myOut.println(LICENCE + "\n");
        break;
    }
    try {
        new NanoHTTPD(new ServerSocket(port), wwwroot);
    } catch (IOException ioe) {
        Log.err(Sys.HTTPD, "Couldn't start server:\n", ioe);
        H2O.exit(-1);
    }
    myOut.println("Now serving files in port " + port + " from \"" + wwwroot + "\"");
    myOut.println("Hit Enter to stop.\n");
    try {
        System.in.read();
    } catch (Throwable t) {
        Log.err(t);
    }
}
Also used : ServerSocket(java.net.ServerSocket)

Example 32 with ServerSocket

use of java.net.ServerSocket in project h2o-2 by h2oai.

the class H2OLauncher method isPortTaken.

private boolean isPortTaken(int port) {
    boolean portTaken = false;
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(port);
    } catch (IOException e) {
        portTaken = true;
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            /* e.printStackTrace(); */
            }
        }
    }
    return portTaken;
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 33 with ServerSocket

use of java.net.ServerSocket in project gitblit by gitblit.

the class GitblitRunnable method areAllPortsFree.

/**
	 * Method used to ensure that all ports are free, if the runnable is used
	 * JUnit test classes. Be aware that JUnit's setUpClass and tearDownClass
	 * methods, which are executed before and after a test class (consisting of
	 * several test cases), may be executed parallely if they are part of a test
	 * suite consisting of several test classes. Therefore the run method of
	 * this class calls areAllPortsFree to check port availability before
	 * starting another gitblit instance.
	 * 
	 * @param ports
	 * @param inetAddress
	 * @return
	 */
public static boolean areAllPortsFree(int[] ports, String inetAddress) {
    System.out.println("\n" + System.currentTimeMillis() + " ----------------------------------- testing if all ports are free ...");
    String blockedPorts = "";
    for (int i = 0; i < ports.length; i++) {
        ServerSocket s;
        try {
            s = new ServerSocket(ports[i], 1, InetAddress.getByName(inetAddress));
            s.close();
        } catch (Exception e) {
            if (!blockedPorts.equals("")) {
                blockedPorts += ", ";
            }
        }
    }
    if (blockedPorts.equals("")) {
        System.out.println(" ----------------------------------- ... verified");
        return true;
    }
    System.out.println(" ----------------------------------- ... " + blockedPorts + " are still blocked");
    return false;
}
Also used : ServerSocket(java.net.ServerSocket)

Example 34 with ServerSocket

use of java.net.ServerSocket in project spring-security by spring-projects.

the class ApacheDSContainerTests method getDefaultPorts.

private List<Integer> getDefaultPorts(int count) throws IOException {
    List<ServerSocket> connections = new ArrayList<ServerSocket>();
    List<Integer> availablePorts = new ArrayList<Integer>(count);
    try {
        for (int i = 0; i < count; i++) {
            ServerSocket socket = new ServerSocket(0);
            connections.add(socket);
            availablePorts.add(socket.getLocalPort());
        }
        return availablePorts;
    } finally {
        for (ServerSocket conn : connections) {
            conn.close();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ServerSocket(java.net.ServerSocket)

Example 35 with ServerSocket

use of java.net.ServerSocket in project camel by apache.

the class HttpTestServer method toString.

@Override
public String toString() {
    // avoid synchronization
    ServerSocket ssock = servicedSocket;
    StringBuilder sb = new StringBuilder(80);
    sb.append("LocalTestServer/");
    if (ssock == null) {
        sb.append("stopped");
    } else {
        sb.append(ssock.getLocalSocketAddress());
    }
    return sb.toString();
}
Also used : ServerSocket(java.net.ServerSocket)

Aggregations

ServerSocket (java.net.ServerSocket)738 IOException (java.io.IOException)337 Socket (java.net.Socket)266 InetSocketAddress (java.net.InetSocketAddress)131 Test (org.junit.Test)118 SocketException (java.net.SocketException)56 InputStream (java.io.InputStream)51 SocketTimeoutException (java.net.SocketTimeoutException)43 OutputStream (java.io.OutputStream)41 InetAddress (java.net.InetAddress)41 BindException (java.net.BindException)28 URL (java.net.URL)28 SSLServerSocket (javax.net.ssl.SSLServerSocket)26 File (java.io.File)24 InputStreamReader (java.io.InputStreamReader)24 UnknownHostException (java.net.UnknownHostException)24 BufferedReader (java.io.BufferedReader)21 SSLSocket (javax.net.ssl.SSLSocket)21 DatagramSocket (java.net.DatagramSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)16