• Post Reply Bookmark Topic Watch Topic
  • New Topic

Buffered Reader problems  RSS feed

 
Sumeet Anand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All ,
I have this method in which I open a socket and then passs an XML file�and also after some processing I receive
that XML file.But the problem is that the program just hangs up when while loop is executed.
I can print the System.outs until------- Message to send is-------.After that the programme just hangs
Up and doesn,t print anything after that and the while loop is also not executed.
**************************************************
String xmlResponseFileName = xmlFileName;
File xmlResponseFile = null;
Socket socket = null;
BufferedReader sin = null;
PrintWriter sout = null;
PrintWriter fout = null;
String line = null;
try {

xmlResponseFile = new File(xmlFileName);
System.out.println("XML response file is"+xmlResponseFile);
fout = new PrintWriter(new FileOutputStream(xmlResponseFile));
socket = new Socket(hostName, port);
sout = new PrintWriter(socket.getOutputStream(), true);
sin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Message to send is"+messageToSend);
sout.println(messageToSend);
while ((line = sin.readLine()) != null){
System.out.println("Line starting is as"+line);
fout.println(line);
}
System.out.println("After the while loop");
fout.flush();
fout.close();
sin.close();
sout.close();
socket.close();
return xmlResponseFile;
}
catch (IOException e) {
System.err.println("ERR - IPCInProcessorAccessor - sendAndReceiveMessage: IOException: " + e);
e.printStackTrace(System.err);
}
Please pull me out of this soup.
Thanks in advance.
Regards,
Sumeet.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show an example of exactly what the output looks like for this program? (The last few lines, at least?)
Do you call System.exit() anywhere in your program? (Especially from another thread?) Does your program use GUI components where the close operation is WindowConstants.EXIT_ON_CLOSE?
Try adding a few more catch blocks:

This is in case an exception is being throw an then caught in some other code somewhere, without any message.
Also put in a somewhere to make sure it hasn't somehow been disabled.
 
Wes B Webster
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while ((line = sin.readLine()) != null){
This line should be:
while ((line = sin.readLine()) != -1) {
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pardon? I think you're confusing readLine() with read(char[]), Wes.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
Try flushing the output stream.. which is printwriter in your case. I havent much exp with printwriter since I prefer bufferedwriter to it.
Regds
LUpo
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!