Mam problem z wysyłaniem obiektu protokołem TCP. Po wysłaniu pierwszym i zmianie, obiekt zostaje wysłany ponownie. Klient jednak wydaje się że uzyskał obiekt bez zmian. ;/
Tak wygląda wysyłanie serwera i pobieranie klienta.
if(client.equals("0")){
c.createServer(50009);
c.waitClient();
Map map = new Map();
map.add();
c.sendObjTCP(map);
System.out.println(map.list.get(0).x+" "+map.list.get(0).y);
for(int i=0;i<5;i++){
map.update();
System.out.println(map.list.get(0).x+" "+map.list.get(0).y);
c.sendObjTCP(map);
}
System.out.println(c.getLogs()); //wyświetla logi
c.disconnect();
}else{
c.joinServer("localhost", 50009);
test =(Map)c.getObjTCP(0);
System.out.println(test.list.get(0).x+" "+test.list.get(0).y);
for(int i=0;i<5;i++){
test =(Map)c.getObjTCP(0);
System.out.println(test.list.get(0).x+" "+test.list.get(0).y);
}
System.out.println(c.getLogs()); //wyświetla logi
c.disconnect();
}
Serwerowi wyświetla:
-20.0 299.0
-18.6722 298.79877
-17.344398 298.59753
-16.016598 298.3963
-14.688797 298.19507
-13.360996 297.99384
null
Klientowi zaś:
-20.0 299.0
-20.0 299.0
-20.0 299.0
-20.0 299.0
-20.0 299.0
-20.0 299.0
null
Tak zaś wyglądają klasy wysyłanych obiektów:
class Map implements Serializable{
ArrayList<Mob> list = new ArrayList<>();
public void add(){
list.add(new Mob());
}
public void update(){
for(Mob m:list){
m.update();
}
}
}
class Mob implements Serializable{
float x,y;
int speed;
int speedforp;
float xsp,ysp;
static int times=1;
Mob(){
randomPos();
}
public void anim(int sx, int sy,int ex, int ey,int speed){
x=sx;
y=sy;
speedforp=speed;
this.speed=speed;
xsp=(ex-sx)/(float)speed;
ysp=(ey-sy)/(float)speed;
}
public void update(){
if(speed>=0){
x+=xsp;
y+=ysp;
speed-=1;
}else{
randomPos();
times++;
}
}
Random r = new Random();
public void randomPos(){
int dir = r.nextInt(4)+1;
int speedi= r.nextInt(300)+200;
if(dir==1){
int y=420;
int x = r.nextInt(600);
int fx= r.nextInt(600);
anim(x, y, fx, -20, speedi);
}
else if(dir==2){
int x=620;
int y = r.nextInt(400);
int fy = r.nextInt(400);
anim(x, y, -20,fy , speedi);
}
else if(dir==3){
int y=-20;
int x = r.nextInt(600);
int fx= r.nextInt(600);
anim(x, y, fx, 420, speedi);
}
else if(dir==4){
int x=-20;
int y = r.nextInt(400);
int fy = r.nextInt(400);
anim(x, y, 620, fy, speedi);
}
}
}
W przypadku wysyłania obiektów innych klas wszystko działa poprawnie np. String lub zwykła Klasa posiadająca różnego typu dane.