0

Trying to work on this assignment for practice. Got stuck few with two issues.

  1. Where should I stop the Thread after printing the request on console? Later I would need to do that after sending the response.
  2. From where should I send the response back? I can easily do it from processRequest(). Was thinking if there is anyway to send a HttpResponse back. Would it be ok to send the response back from HttpRequest class itself?

Code

Main class

public final class WebServer {

    public static void main(String[] args) throws Exception {
        int port = 1983;
        final ServerSocket server = new ServerSocket(port);
        System.out.println("Comes here");
        Socket client = null;
        while (true) {
            client = server.accept();
            System.out.println("Got the connection" + client.toString());
            final HttpRequest request = new HttpRequest(client);
            Thread thread = new Thread(request);
            thread.start();

        }
    }

}

HttpRequest.java

final class HttpRequest implements Runnable {

    Socket socket;

    public HttpRequest(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            processRequest();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void processRequest() throws IOException {
        String headerline = null;
        DataOutputStream out = null;
        BufferedReader in = null;
        out = new DataOutputStream(socket.getOutputStream());
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        while ((headerline = in.readLine()).length() != 0) {
            System.out.println(headerline);
        }
        out.close();
        in.close();
        socket.close();
    }

}

2 Answers 2

1
  1. The thread will terminate as soon as the socket is closed.

  2. To output to the client, in this form, you must generate your own Http header that needs to be sent to the client plus all of your data that you're sending to your client. To do this, you can do:

    out.writeBytes(<HttpHeaderString>);

Then for your file, you can do something like this:

        FileInputStream fileToClient;
        OutputStream toClient;
        byte[] buffer = new byte[2048];
        int bytes = 0;
        
        while ((bytes = fileToClient.read(buffer)) != -1){
            toClient.write(buffer, 0, bytes);
        }
Sign up to request clarification or add additional context in comments.

Comments

1
  1. The page mentions instance of Thread class, but ideally, you don't stop threads, you return them back to the pool. Such that you don't create a new thread for every request but reuse threads.

    pool = Executors.newFixedThreadPool(poolSize);

    while (true) { pool.execute(new HttpRequest(client); }

  2. You can do it from anywhere just keep reference to Socket's OutputStream and don't forget to flush it.

    As for the naming, it's bit awkward to send response back from request object. Just rename your HttpRequest to something like HttpRequestHandler, which assumes that you'll handle incoming request here the way you prefer, and it should be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.