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.