Solved

ObjectOutputStream & Socket

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

785 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