Results 1 to 10 of 10
Thread: PrintWriter problems
- 11-12-2011, 05:16 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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:
This code successfully prints both strings: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 doesn't print anything: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);
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.Java Code:String response = "Hello!"; writer.printf(response); writer.printf("Tracer");
Do you have any suggestions for why this could be happening?
Thanks,
MichaelLast edited by mickylad; 11-12-2011 at 05:20 PM.
-
Re: PrintWriter problems
Do you flush and/or close the writer object?
- 11-12-2011, 05:27 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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 05:29 PM. Reason: typo
-
Re: PrintWriter problems
- 11-12-2011, 05:49 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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.
- 11-12-2011, 08:18 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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:
Then it is used in getStatusLine() as the beginning of the returned response String:Java Code:String requestline = (String) reader.readLine(); ... version = requestline.substring(requestline.lastIndexOf(" ")+1, requestline.length());
And finally printed at some pointed in the thread's run() method: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"); }
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.Java Code:writer.printf(getStatusLine("200"));
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:
All help is hugely appreciated!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()); } } }
- 11-12-2011, 08:52 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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.
-
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.
- 11-12-2011, 09:55 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
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!
- 11-13-2011, 03:23 PM #10
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Need help with PrintWriter!
By PapaEcho in forum New To JavaReplies: 2Last Post: 10-09-2011, 06:51 PM -
printwriter
By sope in forum New To JavaReplies: 2Last Post: 05-09-2011, 07:57 PM -
java PrintWriter
By miko5054 in forum New To JavaReplies: 3Last Post: 05-08-2011, 01:25 PM -
Help with printwriter class
By eel in forum New To JavaReplies: 9Last Post: 09-19-2010, 02:04 PM -
Help with printwriter.
By Addez in forum New To JavaReplies: 2Last Post: 10-30-2009, 01:58 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks