I've written a simple com.sun.net.httpserver.HttpServer program that runs on a non-server mac. When I visit http://localhost:39600/test
in a web browser, it gives me the correct response: "Here's my response!".
I then move this java file to a webserver Mac OS X Snow Leopard managed with Server Admin, compile it, run it on port 39600, visit http://localhost:39600/test
in the webserver's internet browser. It gives the correct response. But when I visit http://webserveraddress:39600/test
it doesn't respond at all. I know it's probably something with server admin and the firewall, but how do I open up port 39600 to outside requests through Server Admin?
Here's the simple, standard java class, though I think it's more an issue with Server Admin.
package server;
import java.io.*;
import java.net.*;
import com.sun.net.httpserver.*;
public class Server {
private static final int SERVER_PORT_NUMBER = 39600;
private static final int MAX_WAITING_CONNECTIONS = 10;
public static void main(String[] args) {
new Server().run();
}
private Server(){
}
private HttpServer server;
private void run(){
configure();
server.createContext("/test", testHandler);
server.start();
}
private HttpHandler testHandler = new HttpHandler(){
@Override
public void handle(HttpExchange t) throws IOException {
//TODO read request body
t.sendResponseHeaders(200, 0); //all ok!
OutputStream os = t.getResponseBody();
os.write("Here's my response!".getBytes());
os.close();
}
};
private void configure(){
try {
server = HttpServer.create(new InetSocketAddress(SERVER_PORT_NUMBER),
MAX_WAITING_CONNECTIONS);
}
catch (IOException e) {
System.out.println("Could not create HTTP server: " + e.getMessage());
e.printStackTrace();
return;
}
server.setExecutor(null); // use the default executor
}
}