Ive seen this question a couple times but I honestly dont understand how to get it to work. I have a client and server application, and the server, has a ObjectOutputStream variable, sends an array list with a double integer array in it (the reason for this is, is because it has to send an object over, and since normal arrays arent objects, I wrap it around an array list so that it is able to send). The client receives it with its ObjectInputStream variable, but it only receives the first instance of the array list object even if the double array changes its numbers. How can I get it to update the object its sending? How can I get the ObjectInputStream to receive a new object everytime?
-Dan
Server:
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: (same for client):
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;
}
Client:
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!");
}
}
}
}
Object
s in Java.) – Tom Hawtin - tackline