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);
}
}
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;
}
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;
}
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();
}
}
}
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();
}
Aggregations