wood burning stoves 2.0*
The moose likes Java in General and the fly likes Help on Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help on Serializable" Watch "Help on Serializable" New topic
Author

Help on Serializable

Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
I have a simple class:

That I write from a client to a server.
The client simply uses an ObjectOutputStream wrapped around
a socket and performs a writeObject(tc);
Ex.

The server simply uses an ObjectInputStream to read in
the object.

Here is my question. When I declare my class TestCommand without
any instance variables it works fine. But then when I give it
a variable, in this case total, I get a dump. Why??
I tried to use defaultWriteObject() and defaultReadObject() but still
received dump - although a different exception.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You should always call flush() on an ObjectOutputStream before trying to read an object from the other end of a pipe like this. That should fix the problem here.


[Jess in Action][AskingGoodQuestions]
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
Still gives me the same old exception. Thank you though!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

OK, well... you didn't tell us what the actual exception is, so I'm just guessing. What does the stack dump say? Does the client have access to the same .class file as the server? Make sure it's not a version-mismatch problem (many people seem to think the .class file is serialized with the data -- this is untrue.)
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
Here I have included more than what you probably want. Client, Server,
and Object code, and the exception.

THE CLIENT:

THE SERVER:

THE OBJECT:

THE EXCEPTION:

Socket error: Connection reset
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2150)
at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2558)
at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2582)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1834)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at testx.tests.ServerTest.begin(ServerTest.java:58)
at testx.tests.ServerTest.main(ServerTest.java:22)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

This code should work perfectly, except for one thing: the client exits right after sending the data, without closing the stream, and this may leave the server surprised to find the connection abruptly terminated. To confirm this, add a "Thread.sleep(5000)" after the flush call in the client, and see if the problem clears up.
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
Yes!!! You are exactly correct. How do I correct the code and can you
give a little more explanation.
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
But, I just thought of something, this does not explain why
it would work everytime with an object that did not have instance
variables. In otherwords, how does the instance variable come into
play?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help on Serializable