• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

Socket problem

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);

while ((line = sin.readLine()) != null){
System.out.println("Line starting is as"+line);

System.out.println("After the while loop");

return xmlResponseFile;
catch (IOException e) {
System.err.println("ERR - IPCInProcessorAccessor - sendAndReceiveMessage: IOException: " + e);
Please pull me out of this soup.
Thanks in advance.
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than pull you out, I'll eat the soup if that is the same with you. I like soup
You're writing and reading in the same thread. That is dangerous in the sense that if both client and server don't agree exactly on when you're writing to the channel and when you're reading, you will end up in a deadlock where both sides are either waiting for each other or both trying to write while the other side isn't listening.
In this particular case, one thing that immediately strikes me is that you are writing the entire file but not immediately closing the output stream. I'm willing to bet that the other side is sitting there waiting for more to come until you actually close that stream.
You can jump two ways.
The first way is to ensure that client and server have the same idea about who's writing at all times. Close the stream to signal the end of the stream, or communicate the content length before you send the actual content.
The second way is to spawn a separate thread that reads the server response. In the end, you would use Thread.join() to join up the read and write threads. This avoids deadlocks, but is more complicated. It seems overkill for your problem.
Mmmm. Lovely soup, that
- Peter
[ January 07, 2003: Message edited by: Peter den Haan ]
ice is for people that are not already cool. Chill with this tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic