Question

I'm coding web-server:

import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class HttpServer {

    public static void main(String[] args) throws Throwable {
        //http://localhost:3000
        ServerSocket ss = new ServerSocket(3000);

        while (true) {
            //Waiting for socket
            Socket s = ss.accept();

            System.out.println("Client accepted");



            //The main process
            new SocketProcessor(s, ss).start();
        }
    }

    private static class SocketProcessor implements Runnable {
        private Thread t;
        private Socket s;
        private InputStream is;
        private OutputStream os;


        private SocketProcessor(Socket s, ServerSocket ss) throws Throwable {
            t = new Thread(this, "Server Thread");
            this.s = s;
            this.is = s.getInputStream();
            this.os = s.getOutputStream();

        }

        public void run() {
            try {
                readInputHeaders();
                writeResponse("<html><body><h1>Hello</h1></body></html>");


            } catch (Throwable t) {
                /*do nothing*/
            } finally {
                try {
                    s.close();
                } catch (Throwable t) {

                }
            }
            System.out.println("Client processing finished");
        }


        public void start() {
            t.start();
        }

        private void writeResponse(String s) throws Throwable {
            String response = "HTTP/1.1 200 OK\r\n" +
                    "Server: Server\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: " + s.length() + "\r\n" +
                    "Connection: close\r\n\r\n";
            String result = response + s;
            os.write(result.getBytes());
            os.flush();
        }

        private void readInputHeaders() throws Throwable {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while (true) {
                int a = br.read();
                System.out.print((char)a);
                if (a==-1) {
                    break;
                }
            }
        }
    }
}

But I've got trouble with reading. This methods takes too much time:

 private void readInputHeaders() throws Throwable {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                while (true) {
                    int a = br.read();
                    System.out.print((char)a);
                    if (a==-1) {
                        break;
                    }
                }
            }

It returns headers and query string, but it freezes my web-server to death. What's wrong?

Was it helpful?

Solution

You need to read, and implement, the HTTP protocol, for which you need to read the RFCs. Guesswork like this will not work. The method you've written to gather the request doesn't begin to be valid. A request can't be followed by EOS, otherwise you can't write the response. You need to look at the content-length header, chunked encoding, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top