Solved

ObjectOutputStream & Socket

Posted on 2001-08-08
5
249 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

23 Experts available now in Live!

Get 1:1 Help Now