Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By mickylad

Thread: PrintWriter problems

  1. #1
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default PrintWriter problems

    I have a PrintWriter with auto-flush set to true, that I am using to write out responses to HTTP requests. For some reason, the PrintWriter will write out fixed strings, but not variables.

    This creates the writer:

    Java Code:
        private Socket socket = null;
        private InputStreamReader input = null;
        private OutputStream output = null;
        private BufferedReader reader = null;
        private PrintWriter writer = null;
    
        HTTPThread(Socket client)
        {
            try {
                
                socket = client;
                
                input = new InputStreamReader(client.getInputStream());
                reader = new BufferedReader(input);
                
                output = client.getOutputStream();
                writer = new PrintWriter(output, true);
                
            } catch (IOException e) {
                
                // Error accessing socket streams
                System.out.print("Error accessing streams on socket: "+ client.getInetAddress() + ":" + client.getPort());
            }
        }
    This code successfully prints both strings:
    Java Code:
                            String response = "Hello!";
                            writer.printf("Tracer");
                            writer.printf(response);
    This code doesn't print anything:
    Java Code:
                            String response = "Hello!";
                            writer.printf(response);
    This code doesn't print anything:
    Java Code:
                            String response = "Hello!";
                            writer.printf(response);
                            writer.printf("Tracer");
    I have no SSCCE, as the class is ~300 lines long. It is really just a number of nested if statements, which decide which parameters to pass to the function that creates the response string.

    Do you have any suggestions for why this could be happening?

    Thanks,
    Michael
    Last edited by mickylad; 11-12-2011 at 06:20 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: PrintWriter problems

    I close the writer, input stream and output stream in that order at the end of the function I call writer.printf() in.

    I've also tried adding calls to flush() before and after calls to printf() despite the auto-flush, but they don't seem to change anything.
    Last edited by mickylad; 11-12-2011 at 06:29 PM. Reason: typo

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: PrintWriter problems

    Quote Originally Posted by mickylad View Post
    I close the writer, input stream and output stream in that order at the end of the function I call writer.printf() in.

    I've also tried adding calls to flush() before and after calls to printf() despite the auto-flush, but they don't seem to change anything.
    Hm, then you may have a bug buried in your code somewhere. An SSCCE would help us to help you.

  5. #5
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: PrintWriter problems

    Ok thank you, I understand. I will take another look at my program and return with an SSCCE if I still can't figure this out.

  6. #6
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: PrintWriter problems

    After a stressful trawl through my code I've found it boils down to a String I have called version. This is assigned from the sub-string after the last white-space character in the request-line, which is read in by the reader:

    Java Code:
        String requestline = (String) reader.readLine();
        ...
        version = requestline.substring(requestline.lastIndexOf(" ")+1, requestline.length());
    Then it is used in getStatusLine() as the beginning of the returned response String:

    Java Code:
    public String getStatusLine(String code) {
        
        String reason = WebServer.responsecodes.getProperty(code);
        
        if(reason == null) {
            
            code = "500";
            reason = "Internal Server Error";
        }
        
        //trace
        System.out.println(version);
        
        return (version + " " + code + " " + reason + "\r\n");
    }
    And finally printed at some pointed in the thread's run() method:

    Java Code:
    writer.printf(getStatusLine("200"));
    At first it was initialized inside the run() method and passed as a parameter to getStatusLine(), but I have now changed it to a public global variable.

    I can assign version to anything as long as it does not begin with "HTTP/2.0" or "HTTP/1.", or as long as something else has been printed before it. I have no idea why this might be, apart from some in-built Java protocol I don't know about, but it's quite an issue for me since all HTTP responses start with the HTTP version!

    Here's an SSCCE, just run it and type localhost:8080 as a URL into your browser:

    Java Code:
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.io.IOException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Tester
    {
        public static void main(String[] args)
        {
            try
            {
                ExecutorService scheduler = Executors.newCachedThreadPool();
                ServerSocket server = new ServerSocket(8080);
                while (true)
                {
                    Socket client = server.accept();
                    Runnable r = new TestThread(client);
                    scheduler.execute(r);               
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    
    import java.io.*;
    import java.net.Socket;
    
    public class TestThread implements Runnable
    {
        private Socket socket = null;
        private OutputStream output = null;
        private PrintWriter writer = null;
        
        TestThread(Socket client)
        {
            try {
                socket = client;
                
                output = client.getOutputStream();
                writer = new PrintWriter(output, true);
                
            } catch (IOException e) {
                
                // Error accessing socket streams
                System.out.print("Error accessing streams on socket: "+ client.getInetAddress() + ":" + client.getPort());
            }
        }
    
        @Override
        public void run()
        {
            try {
                /* These all work */
                //writer.printf("HTTP/2.");
                //writer.printf("HTTP/0.9");     
                //writer.printf("will this work? HTTP/1.");
                //writer.printf("Not a HTTP version..");
                //writer.printf("HTTP/0.9");
                
                /* These don't unless a valid String precedes them */
                //writer.printf("HTTP/1.hellothere");
                //writer.printf("HTTP/1.1");
                writer.printf("HTTP/2.0");
                
                // Close the streams
                writer.close();
                output.close();
                
            } catch (IOException e) {
                
                // I/O exception
                System.out.print("I/O exception on socket: "+ socket.getInetAddress() + ":" + socket.getPort());
            }
        }
    }
    All help is hugely appreciated!

  7. #7
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: PrintWriter problems

    I should say, PrintWriter can write those Strings to System.out, but not to my Socket output stream. Again, I have no idea.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: PrintWriter problems

    I'm sure it writes it to the socket, it's just that the web page probably interprets the text different than you're expecting.

  9. #9
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: PrintWriter problems

    Yeah, I had that thought after posting but I'm still not sure how to solve it. Gonna have to sleep on it, thanks for your input!

  10. #10
    mickylad is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: PrintWriter problems

    The answer was pretty obvious; the browser doesn't write out HTTP requests but acts on them, so it was working but I just couldn't see anything (adding headers or a file after the request would display correctly). Solved!
    Fubarable likes this.

Similar Threads

  1. Need help with PrintWriter!
    By PapaEcho in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 07:51 PM
  2. printwriter
    By sope in forum New To Java
    Replies: 2
    Last Post: 05-09-2011, 08:57 PM
  3. java PrintWriter
    By miko5054 in forum New To Java
    Replies: 3
    Last Post: 05-08-2011, 02:25 PM
  4. Help with printwriter class
    By eel in forum New To Java
    Replies: 9
    Last Post: 09-19-2010, 03:04 PM
  5. Help with printwriter.
    By Addez in forum New To Java
    Replies: 2
    Last Post: 10-30-2009, 02:58 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
  •