• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Buffered Reader problems

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while ((line = sin.readLine()) != null){
This line should be:
while ((line = sin.readLine()) != -1) {
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pardon? I think you're confusing readLine() with read(char[]), Wes.
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic