Reputation: 6842
I'm still learning java and trying to build a basic WebServer using Sockets for now i have the server working and sending output data back to the browser but i'm unsure how to send the headers i thought they were just "\r\n\r\n" from the content body
This is how i'm writing to the browser currently
Socket socket = socketServer.accept();
System.out.println("Web Request From: "+socket.getInetAddress().toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
PrintWriter out = new PrintWriter(socket.getOutputStream());
Stirng output = "<!DOCTYPE html><html><body><h1>403 Forbidden</h1></body></html>"
out.write(output, 0, output.lenght());
out.flush();
out.close();
Upvotes: 0
Views: 281
Reputation: 6842
your forced to send the HTTP information first
out.println("HTTP/1.0 200");
is required before any other codes send or the main body
Upvotes: 0
Reputation: 604
You should take a look at NanoHttpd. Its a one file Web server and its a lot of fun to play with. They are sending headers back, so you should be able to look at their code fairly easily and see what they are doing.
Upvotes: 1
Reputation: 6809
Put the headers in the output string, separated from each other by \r\n
and from the main body by \r\n\r\n
. Or, with the PrintWriter just print them one by one, an empty line, and then the body.
Upvotes: 1