| title | Creating A Server |
|---|---|
| description | Quick start guide for creating a SimpleHttpServer. |
A SimpleHttpServer is created the same way that a native http server is created; using the create method.
SimpleHttpServer server = SimpleHttpServer.create();
// https
SimpleHttpsServer hserver = SimpleHttpsServer.create();For a server to start it must be binded to a port. The port can be either an integer of an InetSocketAddress.
SimpleHttpServer server = SimpleHttpServer.create(8080);SimpleHttpServer server = SimpleHttpServer.create(new InetSocketAddress(8080));If a server is not binded an instantiation then it must be binded using the bind method.
SimpleHttpServer server = SimpleHttpServer.create();
server.bind(8080);SimpleHttpServer server = SimpleHttpServer.create();
server.bind(new InetSocketAddress(8080));A server can only be binded to a port once, it can not be changed after it is set.
If a port is already occoupied by another process then a BindException will be thrown.
The server can be started by using the start method on the server. A server can only be started if it is binded to a port.
SimpleHttpServer server = SimpleHttpServer.create(8080);
server.start();The server can be stopped by using the stop method. This will stop all inbound requests and currently active exchanges. An optional integer parameter can be supplied to specify how long to wait for current exchanges to complete before closing the server.
SimpleHttpServer server = SimpleHttpServer.create(8080);
server.start();
// stop all exchanges
server.stop();SimpleHttpServer server = SimpleHttpServer.create(8080);
server.start();
// stop current exchanges after 10 seconds
server.stop(10);