Solved

ObjectOutputStream & Socket

Posted on 2001-08-08
5
238 Views
Last Modified: 2008-03-17
Hi Expert,

We have an application that writes objects through a Socket.

The Application includes these lines of code:

_objectOutputStream = new ObjectOutputStream(_socket.getOutputStream());
_objectInputStream = new ObjectInputStream(_socket.getInputStream());

_objectOutputStream.writeObject(command);
Object inObject = _objectInputStream.readObject();

In general every thing works lovely and the objects (Command objects) "Ping-Pong" between the Client and the Server brings results for all the tasks.

But, there is a strange problem in one of the commands:

Problem preface:
----------------------------
One of the Command objects contains an object of class C1,
 which contains a collection of objects from class C2,
 which also contains objects of some other classes.
All the classes are "implements Serializable".
This object (of class C1) is fetched successfully (wrapped by a Command object).
This object is also written back to the server successfully (wrapped by another Command object).
This object is a member of a JDialog that lets the modifications of the object content.
Closing this JDialog solve the problem (which will be described on the next line) for the moment.

The Problem:
------------
Any subsequent writing of the object of class C1 (modified for several times) on the Client side,
     _objectOutputStream.writeObject(command);
wrapped by a Command object.
Results with:
Reading constantly (at the Server side) a Command object wrapping the same object that was written by the Client at the first time.
      _objectInputStream.readObject();

During all the object writing attempts at the Client:
     _objectOutputStream.writeObject(command);
The wrapping Command object (at the Client side) contains the current modified object ! ... ???


Thanks in advance,
Oren,
Israel.


0
Comment
Question by:orenm
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:exorcist
ID: 6363889
Hi,

I didnt quite understand your question, but it seems to me, that RMI might be a better alternative when transferring complex objects.
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6366341
Call _objectOutputStream.reset() before writing the object.

ObjectOutputStream keeps track of objects written to it and when an object is sent the second time only the object handle is sent and not the actual object for efficiency reasons and to avoid circular references.

reset() tells it to forget what's already been sent.
0
 

Author Comment

by:orenm
ID: 6368041
Thanks,

(Short and to the point ...)

Oren.
0
 
LVL 92

Expert Comment

by:objects
ID: 6370375
oren,

I hate typing :-)
Thanks for the points.
0
 

Author Comment

by:orenm
ID: 6370402
Hi objects,

there is another question registered with my name:
 
File.delete()

See if you can help again (this time only 100 points ...).

Oren.


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now