I want to send through ObjectStream an object of class: (Packet is Serializable)
public class ServerPlayersListPacket extends Packet {
private static final long serialVersionUID = -7141960214853425631L;
private ArrayList<Player> players;
public ServerPlayersListPacket(ArrayList<Player> players) {
this.setPlayers(players);
}
public ArrayList<Player> getPlayers() {
return new ArrayList<Player>(Collections.synchronizedList(players));
}
public void setPlayers(ArrayList<Player> players) {
this.players = players;
}
}
But there is a problem with ArrayList. While reciving it from the other site I got OptionalDataException.
I found it’s issue related to ArrayList synchronization. But how to make it work?
My trick is:
ServerPlayersListInitPacketServerNextPlayerPacketin loop with onePlayerperPacketThis solution also allows client site to update players list dynamically which is more smooth.