0

I am writing a Java Server that outputs a request number in browser. It is a multithreaded application, that suppose to create a new thread for each request and output the next number. It does work, however the number output is incremented by 4 when i have only one increment. So i have a feeling that server somehow keeps receiving the requests without outputting them in the browser.

public class ResponseServer {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int clientNum = 0;
    ServerSocket serverSocket = new ServerSocket(8000);
    Socket socket = null;
    while (true) {
        socket = serverSocket.accept();
        System.out.println("CLIENT NUM " + clientNum);
        new HandleClient(socket, clientNum++).run();
        System.out.println("DONE");
    }
}
static class HandleClient implements Runnable {
    Socket socket;
    int counter;

    public HandleClient(Socket socket, int counter) {
        System.out.println("RECIEVED " + counter);
        this.socket = socket;
        this.counter = counter;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            InputStream in = socket.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            OutputStream out = socket.getOutputStream();
            String response = "<h2>" + counter + "</h2>";
            out.write("HTTP/1.1 200 OK\n".getBytes());
            out.write("Content-Type: text/html; charset=utf-8\n".getBytes());
            out.write(("Content-Length: " + response.length() + "\n\n").getBytes());
            out.write(response.getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

}

}

Jenny
  • 445
  • 2
  • 6
  • 23

1 Answers1

0

There is a bug in Chrome and IE browsers. There is no problem with code - works if you use Firefox

Jenny
  • 445
  • 2
  • 6
  • 23