0

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
    }
}
ejsuncy
  • 403
  • 1
  • 5
  • 7

2 Answers2

0

This is most likely caused by a closed port on the router, although it could be a firewall on the web-server. You need to open up port 39600 on the router for the server's network - this is known as port-forwarding.

hesson
  • 101
  • 1
  • This server isn't behind a router. It has a static IP address. I'm looking for specific Server Admin help. It's the software that manages everything to do with the server on Mac OS X 10.6.8. I just don't know how to allow that port to be open. – ejsuncy Aug 10 '13 at 18:12
0

It turns out that Server Admin doesn't manage specific ports. The GUI for the Snow Leopard firewall only lets you open specific applications (and thereby their associated port numbers). To open a specific port, you have to dig in to Terminal and type sudo ipfw add <rulenumber> allow tcp from any to any dst-port <portnum> where <rulenumber> is between 00001 and 65535 and <portnum> is as well (there are reserved port numbers...you can see which ones are with sudo ipfw list.

ejsuncy
  • 403
  • 1
  • 5
  • 7