public class HTTPServer extends Object implements Runnable
This class provides a multithreaded HTTP server that can:
The server must be initialized using open(InetAddress, int) or open(String, int) before starting. Optionally, HTTPRequestListener instances
can be registered to handle incoming requests.
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_PORT |
static int |
DEFAULT_TIMEOUT
Default timeout connection for HTTP comunication
|
static String |
NAME
Constants
|
protected int |
timeout
Store the current TCP timeout value The variable should be accessed by getter and setter
metho
|
static String |
VERSION |
| Constructor and Description |
|---|
HTTPServer()
Creates a new HTTP server instance.
|
| Modifier and Type | Method and Description |
|---|---|
Socket |
accept()
Accepts an incoming connection from the server socket.
|
void |
addRequestListener(HTTPRequestListener listener)
Adds a request listener to receive HTTP request notifications.
|
boolean |
close()
Closes the server socket and cleans up resources.
|
String |
getBindAddress()
Gets the bind address as a string.
|
int |
getBindPort()
Gets the bind port.
|
static String |
getName() |
ServerSocket |
getServerSock()
Gets the server socket.
|
int |
getTimeout()
Gets the current socket timeout value.
|
boolean |
isOpened()
Checks if the server socket is opened.
|
boolean |
open(InetAddress addr,
int port)
Opens the server on specified address and port.
|
boolean |
open(String addr,
int port)
Opens the server on specified port with default bind address.
|
void |
performRequestListener(HTTPRequest httpReq)
Notifies all registered request listeners of an HTTP request.
|
void |
removeRequestListener(HTTPRequestListener listener)
Removes a request listener.
|
void |
run() |
void |
setTimeout(int timeout)
Sets the socket timeout value.
|
boolean |
start()
Starts the HTTP server thread.
|
boolean |
stop()
Stops the HTTP server thread.
|
String |
toString()
Returns the bind address as string representation.
|
public static final int DEFAULT_PORT
public static final int DEFAULT_TIMEOUT
public static final String NAME
protected int timeout
public static final String VERSION
public HTTPServer()
public Socket accept()
public void addRequestListener(HTTPRequestListener listener)
listener - the request listener to addpublic boolean close()
public String getBindAddress()
public int getBindPort()
public static String getName()
public ServerSocket getServerSock()
public int getTimeout()
public boolean isOpened()
public boolean open(InetAddress addr, int port)
addr - bind address for the serverport - port number to bind topublic boolean open(String addr, int port)
addr - bind address as hostname or IP address stringport - port number to bind topublic void performRequestListener(HTTPRequest httpReq)
httpReq - the HTTP request to notify listeners aboutpublic void removeRequestListener(HTTPRequestListener listener)
listener - the request listener to removepublic void setTimeout(int timeout)
timeout - timeout in millisecondspublic boolean start()
public boolean stop()