Mini Java Socket Server Example
This Example shows how to create a small HTTP server using Java Sockets. It can be useful to communicate with Cron jobs, to test whether Java is working, check out HTTP communications with a specific port, see all the headers sent by the web browser, or to verify that the post data is being sent to a specific port.
It is also useful when you need a web interface are you are not allowed to install an application server or you just don't want to use and embedded server such us Jetty.
It has the following features.
- Doesn't need external libraries.
- Retrieves post data from the request.
- Uses one thread per request.
- Retrieves all the post data into a char array.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class MiniServerSocketExample { private static final int PORT = 8080; public static void main(String[] args) { try { ServerSocket server = new ServerSocket(PORT); System.out.println("MiniServer active " + PORT); while (true) { new ThreadSocket(server.accept()); } } catch (Exception e) { } } } class ThreadSocket extends Thread { private Socket insocket; ThreadSocket(Socket insocket) { this.insocket = insocket; this.start(); } @Override public void run() { try { InputStream is = insocket.getInputStream(); PrintWriter out = new PrintWriter(insocket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String line; line = in.readLine(); String request_method = line; System.out.println("HTTP-HEADER: " + line); line = ""; // looks for post data int postDataI = -1; while ((line = in.readLine()) != null && (line.length() != 0)) { System.out.println("HTTP-HEADER: " + line); if (line.indexOf("Content-Length:") > -1) { postDataI = new Integer( line.substring( line.indexOf("Content-Length:") + 16, line.length())).intValue(); } } String postData = ""; // read the post data if (postDataI > 0) { char[] charArray = new char[postDataI]; in.read(charArray, 0, postDataI); postData = new String(charArray); } out.println("HTTP/1.0 200 OK"); out.println("Content-Type: text/html; charset=utf-8"); out.println("Server: MINISERVER"); // this blank line signals the end of the headers out.println(""); // Send the HTML page out.println("<H1>Welcome to the Mini Server</H1>"); out.println("<H2>Request Method->" + request_method + "</H2>"); out.println("<H2>Post->" + postData + "</H2>"); out.println("<form name=\"input\" action=\"form_submited\" method=\"post\">"); out.println("Username: <input type=\"text\" name=\"user\"><input type=\"submit\" value=\"Submit\"></form>"); out.close(); insocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
To verify that It's working
Type http://localhost:8080 in your webbrowser:
Possible Errors
- If the port is already being used you will get a java.net.BindException: Address already in use
- If you are using a firewall or the user you used to run the java sample doesn't have enough privileges you will get a java.net.BindException: Permission denied
Notes
- I have tested it with over 2 million requests over 5 months with no problems.
- Change the Content type header if you wish to use it to serve other contents such us JSON, MP3, JPEGs, etc. ex. for mp3 files "Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3"