4

I am trying to implement a failover system. So I have created a monitoring server which is suppose to route the request to different application server which is alive.

So far I have designed a small monitoring server and trying to route the request using HTTP/1.1 302 Found\r\n status code. But I am not able to redirect to my virtual machine although I can access that directly on my browser by typing the url.

Routing code -

class Connection extends Thread{
    Socket clientSocket;
    BufferedReader din;
    OutputStreamWriter outWriter;

    public Connection(Socket clientSocket){
        try{
            this.clientSocket = clientSocket;
            din = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "ASCII"));
            outWriter = new OutputStreamWriter(clientSocket.getOutputStream());
            this.start();
        }catch(IOException e){
            System.out.println("Connection: " + e.getMessage());
        }   
    }
    public void run(){
        try{
        String line = null;
        while((line = din.readLine())!=null){
            System.out.println("Read" + line);
            if(line.length()==0)    
                break;
        }
        //here write the content type etc details:
        System.out.println("Someone connected: " + clientSocket);

        }catch(EOFException e){
            System.out.println("EOF: " + e.getMessage());
        }
        catch(IOException e){
            System.out.println("IO at run: " + e.getMessage());
        }finally{
            try{
                routeRequest(outWriter);
                outWriter.close();
                clientSocket.close();
            }catch(IOException e){
                System.out.println("Unable to close the socket");
            }
        }
    }

    public void routeRequest(OutputStreamWriter outWriter){
        try {
            outWriter.write("HTTP/1.1 302 Found\r\n");
            outWriter.write("Date: Tue, 11 Jan 2011 13:09:20 GMT\r\n");         
            outWriter.write("Content-type: text/plain\r\n");
            outWriter.write("Server: vinit\r\n");           
            outWriter.write("Location: http://192.168.74.128:8080/Stateless/index.html");
            outWriter.write("Connection: Close");
            outWriter.write("\r\n\r\n");
//          String responseStr = "<html><head><title>Hello</title></head><body>Hello world from my server</body></html>\r\n";
//          outWriter.write(responseStr);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

But on browser instead of routing I get No data received page.

Please let me know if I am doing something wrong.

PS: I a able to display dummy hello world page if I go with status 200

Thanks in advance.

8
  • Why don't you just use a ready made solution? Commented Aug 29, 2014 at 10:42
  • sorry but how to do that? Also i really want to do this using sockets so if you can please help me Commented Aug 29, 2014 at 10:46
  • 1
    You don't write application servers yourself (hopefully), so there's no reason to write load balancers either. Commented Aug 29, 2014 at 10:48
  • Kayaman, I don't but this is my assignment and i have to do it. So Can you please help me Commented Aug 29, 2014 at 11:06
  • I have edited the question with whole code. Commented Aug 29, 2014 at 11:29

1 Answer 1

3

Your Location: header lacks an end of line indicator.

outWriter.write("Location: http://192.168.74.128:8080/Stateless/index.html\r\n");

Missing this will cause your Connection: header to become part of the redirected URL. The client will remain connected to the server, unless your server is closing explicitly after delivering this response.

Even if the client times out and closes the connection, it might treat the URL as invalid. If it does redirect, then the page will not be found, since the path would be something like:

/Stateless/index.htmlConnection: Close

Sign up to request clarification or add additional context in comments.

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.