我已经看过几次这个问题,但老实说我不明白如何让它发挥作用。我有一个客户端和服务器应用程序,并且服务器有一个ObjectOutputStream变量,发送一个带有双整数数组的数组列表(原因是,因为它必须发送一个对象,因为正常数组不是对象,我将它包装在一个数组列表中,以便它能够发送)。客户端使用其ObjectInputStream变量接收它,但它只接收数组列表对象的第一个实例,即使double数组更改其数字也是如此。如何让它更新它发送的对象?我怎样才能让ObjectInputStream每次都接收一个新对象?
-Dan
服务器:
public class Send extends Thread{
Starter start;
PlayerArray array;
Names name;
public Send(Starter start, PlayerArray array, Names name){
this.start = start;
this.array = array;
this.name = name;
}
public void run(){
while(start.running){
for(int i = 0; i < start.roomNum; i++){
if(start.send[i] != null && start.OOS[i] != null){
try{
array.setSendingArray(i);
start.OOS[i].writeObject(array.get());
start.OOS[i].flush();
}catch(Exception e){
}
}
}
}
}
}
Server Player Array Class :(对于客户端来说相同):
public class PlayerArray{
Starter start;
int[][] players;
int[][] sending;
public PlayerArray(Starter start){
this.start = start;
players = new int[start.roomNum][2];
defaultArray();
}
public int[][] getArray(){
return players;
}
public ArrayList<int[][]> get(){
ArrayList<int[][]> xyWrap = new ArrayList<int[][]>();
xyWrap.add(sending);
return xyWrap;
}
public void set(int[][] xy){
players = xy;
}
public void set(int index, int newX, int newY){
players[index][0] = newX;
players[index][1] = newY;
}
public void setSendingArray(int i){
//take out "i"'s x and y.
sending = players;
//sending[i][0] = -1;
//sending[i][1] = -1;
}
public void defaultArray(){
for(int i = 0; i < start.roomNum; i++){
players[i][0] = -1;
players[i][1] = -1;
}
sending = players;
}
客户端:
public class Recieve extends Thread{
Starter start;
PlayerArray array;
Names name;
public Recieve(Starter start, PlayerArray array, Names name){
this.start = start;
this.array = array;
this.name = name;
}
public void run(){
while(start.running){
try{
ArrayList<int[][]> xy = new ArrayList<int[][]>();
xy = (ArrayList<int[][]>)start.OIS.readObject();
array.set(xy.get(0));
int[][] testXY = array.getArray();
System.out.println("X: " + testXY[0][0] + " Y: " + testXY[0][1]);
}catch(Exception e){
System.err.println("CANNOT READ OBJECT!");
}
}
}
}
答案 0 :(得分:1)
当您序列化对象时,它会使用令牌替换每个引用。这意味着如果您引用相同的对象两次或两个对象相互引用,它仍将(de)正确地序列化对象。
这也意味着
解决这两个问题的方法是在ObjectOutputStream上调用reset()
。这会清除缓存,防止消耗内存的对象并强制它发送已发送的任何对象,然后重新发送。