Sending a Serialized ArrayList through a Socket using ObjectOutputStream causes loss of Data
Author |
Message |
deltamanx
|
Posted: 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:
code: |
public void sendData()
{
try
{
oos.writeUnshared(getPlayer1());
getPlayer1().getBattleField().trace();
}
catch (Exception e)
{
//Handled elsewhere.
}
}
|
Like wise, here's the respective code snippet for the Client application:
code: |
temp = ois.readObject();
if (temp != null)
{
if (temp instanceof String)
{
Program.getTextWindow().showSystemText((String)temp);
}
else if (temp instanceof Player)
{
//Update player data.
Program.p2 = (Player)temp; //The Player class contains the BattleField class which is the ArrayList in question. It once again is Serializable.
Program.getOpponentWindow().updateAll();
}
//More code cut off.
|
A bit of code from the OpponentWindow Class: (It's not truly relevant).
code: |
public void updateAll()
{
bd.removeAll();
gd.removeAll();
bd.populate(player.getBattleField());
player.getBattleField().trace();
gd.populate(p.getGrave());
}
|
The BattleField Class:
code: |
import java.util.*;
import java.io.*;
public class BattleField extends ArrayList<Card>
implements Serializable, CardHolder
{
public static final long serialVersionUID = 13542L;
public BattleField()
{
super();
}
public Card getCard(int index)
{ return get(index); }
public void trace()
{
System.out.println ("Tracing incoming cards");
for (int i = 0; i < size(); i++)
System.out.println (getCard(i));
}
}
|
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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
deltamanx
|
Posted: 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.
code: |
else if (temp instanceof Player)
{
//Update player data.
Program.p2 = (Player)temp; //The Player class contains the BattleField class which is the ArrayList in question. It once again is Serializable.
Program.getOpponentWindow().updateAll();
} |
I fixed the problem by replacing it with this:
code: |
else if (temp instanceof Player)
{
//Update player data.
Program.p2 = ((Player)temp).clone(); //The Player class contains the BattleField class which is the ArrayList in question. It once again is Serializable.
Program.getOpponentWindow().updateAll();
} |
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. |
|
|
|
|
|
|
|