Getting TCP server to show pages
Hi, I am running a simple TCP server that when I enter 127.0.0.1:8080 gives me "GET / HTTP/1.1".
This is how the server sends that response to the browser:
How can I get that to show the html page in the browser, which I have in the projects folder, by typing 127.0.0.1:8080/index.html?
Socket clientHandler = serverSocket.accept();
DataOutputStream writer = new DataOutputStream(clientHandler.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(clientHandler.getInputStream()));
String response = reader.readLine();
writer.writeBytes(response + "\n");
Re: Getting TCP server to show pages
I solved this by making a Path to the file requested and converting that that Path to bytes that I add to an array like this:
byte fileAsBytesInArray = Files.readAllBytes(filePath);
I then use that array to use reader.writeBytes(new String(fileAsBytesInArray) + "\r\n");
If anybody else are interested in the half solution, because it cant read images this way.