• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can Socket.getInputStream() block

 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to find an answer to the following weird condition.


I have written Dispatcher class that accepts client requests and forwards them to appropriate server instances. There are 3 server instances.

The code is some what as follows



However the program just hangs while trying to do s.getInputStream() above. The server is running and exeuctes fine.

Why does the program block at

[ April 22, 2005: Message edited by: Naveen Sampra ]
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The ObjectInputStream constructor blocks until it completes reading the serialization stream header. Code which waits for an ObjectInputStream to be constructed before creating the corresponding ObjectOutputStream for that stream will deadlock, since the ObjectInputStream constructor will block until a header is written to the stream, and the header will not be written to the stream until the ObjectOutputStream constructor executes. This problem can be resolved by creating the ObjectOutputStream before the ObjectInputStream, or otherwise removing the timing dependency between completion of ObjectInputStream construction and the creation of the ObjectOutputStream.

Java Object Serialization Specification
 
Naveen Sampra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input. I'm really perplexed with this problem.

In my code above as suggested in the quote, ObjectOutputStream is created "before" the ObjectInputStream. So why does ObjectInputStream still block ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic