Search code examples
javasocketsgoogle-chromehttpserversocket

Why Chrome is not rendering the page which it gets from socket output stream? Java


Here is my code:

import java.net.*;
import java.io.*;

class Server
{
     public static void main(String args[])
     {
          try
          {
                ServerSocket svr = new ServerSocket(8900);
                System.out.println("waiting for request");
                Socket s = svr.accept();
                System.out.println("got a request");
                InputStream in = s.getInputStream();
                OutputStream out = s.getOutputStream();

                int x;
                byte data[]= new byte[1024];

                x = in.read(data);

                String response  = "<html><head><title>HTML content via java socket</title></head><body><h2>Hi! Every Body.</h2></body></html>";
                out.write(response.getBytes());
                out.flush();

                s.close();
                svr.close();
                System.out.println("closing all");
          }
          catch(Exception ex)
          {
                System.out.println("Err : " + ex);
          }
     }
}

Running it I hope to go to Chrome right this : 127.0.0.1:8900 and get nicely looking piece of html, but actually Chrome is saying the following :

This page isn’t working 127.0.0.1 sent an invalid response. ERR_INVALID_HTTP_RESPONSE.

Whereas my Server.java is running as I want it to. The console in Eclipse says nicely after connection :

waiting for request got a request closing all.

So I am very stuck. Help me to figure that out, please.


Solution

  • The response you are writing is certainly not readable to chrome. Because it doesn't contain any information about response in header

    Your code is actually send response. You can check it by using curl. Following code will help you to get the response in chrome.

            ServerSocket svr = new ServerSocket(8900);
            System.out.println("waiting for request");
            Socket s = svr.accept();
            System.out.println("got a request");
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
    
            int x;
            byte data[] = new byte[1024];
    
            x = in.read(data);
    
            String t = "HTTP/1.1 200 OK\r\n";
            byte[] bb = t.getBytes("UTF-8");
            out.write(bb);
    
            t = "Content-Length: 124\r\n";
            bb = t.getBytes("UTF-8");
            out.write(bb);
            t = "Content-Type: text/html\r\n\r\n";
            bb = t.getBytes("UTF-8");
            out.write(bb);
    
            String response = "<html><head><title>HTML content via java socket</title></head><body><h2>Hi! Every Body.</h2></body></html>";
            out.write(response.getBytes("UTF-8"));
    
            t = "Connection: Closed";
            bb = t.getBytes("UTF-8");
            out.write(bb);
    
            out.flush();
    
            s.close();
            svr.close();
            System.out.println("closing all");
    

    The vital thing if you change your response then you have to calculate the Content-Length: as it will be the length of your response byte[] and the byte[] of Connection: Closed string.