Results 1 to 2 of 2
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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:

    Java Code:
    while(true)
    {
    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");
    clientSocket.close();
    }
    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?
    Last edited by Gatsu; 02-10-2013 at 03:22 AM.

  2. #2
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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.
    Last edited by Gatsu; 02-10-2013 at 09:58 AM.

Similar Threads

  1. How to show loading while moving between pages
    By maas in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-11-2010, 01:52 PM
  2. java server pages
    By abinesh.s in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-10-2009, 09:03 AM
  3. Java Server Pages
    By ninian in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-20-2008, 03:49 AM
  4. Java Server Pages (II)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 03-12-2008, 07:03 PM
  5. Java Server Pages (I)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 02-17-2008, 12:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •