Computer Science Canada Sending a Serialized ArrayList through a Socket using ObjectOutputStream causes loss of Data |
Author: | deltamanx [ Mon Apr 25, 2011 8:43 pm ] | ||||||||
Post subject: | Sending a Serialized ArrayList through a Socket using ObjectOutputStream causes loss of Data | ||||||||
I am currently working on a program that uses Java's Object Input and Output Streams to transfer Object between an Host and a Client. Unfortunately I have run into a slight problem. For one aspect of the program, I have to send over an ArrayList (which implements the Serializable interface) which contains Object all of the same Class (which also implements the Serializable interface). However when the ArrayList arrives Client-Side, it's completely empty (not null, just empty). To make sure and rule out human error, I traced the content of the ArrayList just before it is sent, and it is not in fact empty. Here's a brief snippet of code for the Server:
Like wise, here's the respective code snippet for the Client application:
A bit of code from the OpponentWindow Class: (It's not truly relevant).
The BattleField Class:
I'm personally clueless on what's causing this problem. Then again it's probably something blatantly obvious. That's usually the case with me and this type of problem. Thanks in advance. |
Author: | deltamanx [ Wed Apr 27, 2011 6:17 pm ] | ||||
Post subject: | Re: Sending a Serialized ArrayList through a Socket using ObjectOutputStream causes loss of Data | ||||
Found my problem.
I fixed the problem by replacing it with this:
I implemented the Cloneable interface in the Player class, an oveerode the method, which returns a shadow copy of the Player Object, with all fields initialized on the local machine as copies of the values received from the remote peer. Hopes this helps someone else. |