ObjectInputStream problem
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   george_shand
Posted On:   Thursday, January 1, 2004 06:48 AM

Hi Does anyone know of a problem involving ObjectInputStreams that affects the functionality of other input and output streams? My problem is this: I have developed a simple client/server program that connects using sockets. I am currently experimenting with the transfer of strings and serialized objects using PrintWriters/BufferedReaders and ObjectOutputStreams/ObjectInputStreams respectively. These objects, along with the socket, are all created inside a single 'createNetworkConnections' method, the method is called by the class constructor. A separate test method has been defined that then tests the functionality of the objects, this method is called inside the 'main' method. When I    More>>

Hi

Does anyone know of a problem involving ObjectInputStreams that affects the functionality of other input and output streams?

My problem is this:

I have developed a simple client/server program that connects using sockets. I am currently experimenting with the transfer of strings and serialized objects using PrintWriters/BufferedReaders and ObjectOutputStreams/ObjectInputStreams respectively.

These objects, along with the socket, are all created inside a single 'createNetworkConnections' method, the method is called by the class constructor.

A separate test method has been defined that then tests the functionality of the objects, this method is called inside the 'main' method.

When I run the program none of the streams appear to work until I comment out the creation of the ObjectInputStream object, then the others function perfectly, passing strings between both client and server and passing an object from client to server.

However when I remove the comments in order to create a new ObjectInputStream object it adversely affects the operation of the others, to put it simply they no longer work. There is no error, they just don't appear to work. Only by commenting out the ObjectInputStream create statement will the others start working again.

I have discovered that I can work around this problem by moving the creation of the ObjectInputStream object from the 'createNetworkConnections' method to the point in the test method when it is actually required.

The problem is driving me mad, any advice would be most welcome.

Thanks
George

   <<Less

ObjectInputStream creation reads bytes from the underlying stream.

Posted By:   Steven_Pearson  
Posted On:   Thursday, January 1, 2004 07:27 AM

Hi,


The ObjectInputStream constructor tries to read header bytes from the underlying stream.


I suspect you have code like this


---8<---
//client
s = new Socket();
in = new ObjectInputStream( s.getInputStream() );
out = new ObjectOutputStream( s.getOutpuStream() );
---8<---

---8<---
//server
s = serverSocket.accept();
in = new ObjectInputStream( s.getInputStream() );
out = new ObjectOutputStream( s.getOutpuStream() );
---8<---


The ObjectInputStream constructors both try to read header bytes from the stream and BLOCKS.


As the read is blocked the ObjectInputStream will never complete construction.


An ObjectOutputStream must be attached to the opposite end of the socket before the ObjectOutputStream is constrected.


During construction the ObjectOutputStream writes the header tot he underlying stream ready for the ObjectInputStream at the opposite end to pick it up.

About | Sitemap | Contact