• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket Programming

 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing a simple client-server program using sockets. I have a server which accepts a search query(String) from clients through a socket. After establishing connection ( i make it sure by printing message on the server) i write a query string on the client's out stream(PrintWriter). The server reads the query and is displaying the query on the server(using 'in' a bufferedreader object). This part is working fine. But then I write back an scknowledgment on the out stream of the server and try to read it on the client with (readLine) function, the program just freezes doing nothing.
Can anyone please help me out: When does a bufferedreader.readline() stall
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one thing, readLine reads until it sees a line terminator, are you sure that the outstream ends with a terminator and that the output stream is flushed?
Bill
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you send data over a socket which must be read immediately at the other end, you must (explicitly or implicitly) call flush() on the underlying OutputStream. If you don't, the data may be buffered locally, causing the other end of the connection to hang waiting for data.

When you construct a PrintWriter, you can specify, using an optional boolean constructor argument, that println() should automatically call flush(). It doesn't by default, and most likely, that's your problem. Just add a "true" argument to your PrintWriter (or PrintStream) constructor call, and things should start working.

We have a forum devoted to socket programming here; I will move this thread there for you for any further discsussion.
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, that was exactly the problem. Actually I had the autoflush value set to true, but while debugging some other things I mistakenly changed that. This stupid thing has cost me one complete day.
Anyways thank you very much again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic