Create a Simple HTTP Server in Java/Socket – Example

Create a Simple HTTP Server in Java/Socket – Example

This tutorial guides you on how to create a Simple HTTP Server in Java Socket Programming with example. When you run this Java program it will start a simple HTTP Server listening to port 8082.

Create a Simple HTTP Server in Java/Socket – Example

Let’s create a simple HTTP Server using Java Socket programming which will listen to HTTP request on port 8082.

To create a simple HTTP Server follow the below steps.

1: First, create a network socket which can accept connection on some specific TCP port let’s say 8082. Using ServerSocket java class you can create socket as shown below.

ServerSocket server = new ServerSocket(8082);

2: After that, you need to write code as shown below, so that it can accept incoming connections. Note, this is the code which returns the Socket object and which can be used later to read the content of the client request.

Socket clientSocket = server.accept();

3: Finally, you can use the Socket object returned and read the content of the client request by using getInputStream() method on the Socket object. Note, you can use BufferedReader to read the client request as it will sent content in multiple line.

InputStreamReader isr =  new InputStreamReader(clientSocket.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();

Here is the complete code to read the client HTTP request in your Simple HTTP Server.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
 
public class SimpleHTTPServer {
 
    public static void main(String args[] ) throws IOException {
 
        //STEP 1: Create a network socket                  
        ServerSocket server = new ServerSocket(8082);
        System.out.println("Listening for connection on port 8082 ...");
 
        while (true) {
            //STEP 2: Accept incoming connection
            Socket clientSocket = server.accept();
            
            //STEP 3: Read content of the client request  
            InputStreamReader isr =  new InputStreamReader(clientSocket.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            String line = reader.readLine();            
            while (!line.isEmpty()) {
                System.out.println(line);
                line = reader.readLine();
            }
 
        }
    }
 
}

The above code does not send any response back to the client. But you can send some sample response like “Welcome” message as shown below.

String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + "Welcome"; 
socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));

Test Simple HTTP Server

You can use Telnet client to test the connection to Simple HTTP Server for a specific port as shown below.

Note, I copied this code via Google Cloud Shell and tried running this in Google Compute Engine VM instance. The IP Address shown here is an External IP Address of an GCP Compute Engine VM.

To test from windows machine make sure that you enable telnet and try testing as shown below.

Create a Simple HTTP Server in Java/Socket – Example

 

You can also test from Linux machine as shown below.

$ telnet 35.223.9.136 8082
 
Trying 35.223.9.136...
Connected to 35.223.9.136.
Escape character is '^]'.

To learn how to run this Simple HTTP Server on Google Cloud Compute Engine VM. Please check this link Run Simple HTTP Server on Google Cloud VM Instance.

That’s it you had learnt how to create a Simple HTTP Server in Java/Socker with Example. Hope it helped 🙂

References

guest
0 Comments
Inline Feedbacks
View all comments