• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on Serializable

 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still gives me the same old exception. Thank you though!!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes!!! You are exactly correct. How do I correct the code and can you
give a little more explanation.
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic