Witam.
Problem polega na tym, że podczas serializacji i wysyłania obiektu przez gniazda jego składowe(pola) zostają niezmienne pomimo ich zmiany. Przed wysłaniem obiektu sprawdzam jego zawartość i jasno wyświetla mi, że jest zmienione lecz po odczytaniu wyświetlana jest wartość z pierwszej serializacji(i taka zostaje do końca).
metody:
public void writeSerializedObject(Gamer gamer) {
try {
oos.writeObject(gamer);
oos.flush();
System.err.println("Wysłano obiekt");
} catch (IOException e) {
System.err.println("Błąd wysyłania obiektu");
}
}
public void readSerializedObject(Gamer gamer){
try {
gamer = (Gamer)ois.readObject();
System.err.println("po odebraniu " +gamer.toString());
} catch (IOException e) {
} catch (ClassNotFoundException e) {
}
}
w konstruktorze tworzę:
public Server(Socket s) throws IOException {
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.err.println("-->Stworzono BufferedReader dla wątku");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
System.err.println("-->Stworzono PrintWriter dla wątku");
oos = new ObjectOutputStream(socket.getOutputStream());
System.err.println("-->Stworzono ObjectOutputStream dla wątku");
ois = new ObjectInputStream(socket.getInputStream());
System.err.println("-->Stworzono ObjectInputStream dla wątku");
start();
System.err.println("-->Stworzono wątek dla klienta");
}
potem w wątku odbieram dane od klienta:
@Override
public void run() {
Gamer gamer = null;
while (true) {
readSerializedObject(gamer);
}
}
klasa Gamer:
package siec;
import java.io.Serializable;
public class Gamer implements Serializable {
private static final long serialVersionUID = 1L;
public int x;
public int y;
public int power;
public int angle;
public int bomb_x;
public int bomb_y;
public Gamer() {
//this.x = 1;
//this.y = 2;
//////itp
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setPower(int power) {
this.power = power;
}
public void setAngle(int angle) {
this.angle = angle;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getPower() {
return power;
}
public int getAngle() {
return angle;
}
public String toString() {
return "x=" + x + " y=" + y + " power=" + power + " angle=" + angle;
}
}
Wydaje mi się, że wszystko jest ok.. Jakieś sugestie? Z góry dziękuje za pomoc.