Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket problem  RSS feed

Sumeet Anand
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.
Peter den Haan
Ranch Hand
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!