客户端中未反映服务器中对象的更改

时间:2015-04-30 15:21:50

标签: java sockets

我的客户端类中有一个TestEllipse对象,它扩展了发送到服务器的Ellipse2D.Double。服务器调用其updatePosition方法,将其包装在ArrayList中,并将ArrayList发送到客户端。 updatePosition将椭圆的x坐标加10。

奇怪的是,即使调用了updatePosition,当客户端在ArrayList中接收到椭圆时,其位置的x坐标似乎也没有变化。更新的位置显示在服务器中......

[TestEllipse[x=60,y=250]]
[TestEllipse[x=70,y=250]]
[TestEllipse[x=80,y=250]]
[TestEllipse[x=90,y=250]]
[TestEllipse[x=100,y=250]]

...但不在客户端:

[TestEllipse[x=60,y=250]]
[TestEllipse[x=60,y=250]]
[TestEllipse[x=60,y=250]]
[TestEllipse[x=60,y=250]]
[TestEllipse[x=60,y=250]]

为什么会这样,我如何才能在客户端显示更新的职位?

在服务器端:

import java.awt.geom.Ellipse2D;
import java.io.*;
import java.net.*;
import java.util.*;

public class TestServer {
    public static void main(String[] args) {
        try {
            List<TestClient.TestEllipse> list = new ArrayList<TestClient.TestEllipse>();
            ServerSocket listener = new ServerSocket(31362);
            Socket socket = listener.accept();
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            while (true) {
                try {
                    TestClient.TestEllipse e = (TestClient.TestEllipse) ois.readObject();
                    if (e != null)
                        list.add(e);
                    for (TestClient.TestEllipse ellipse : list)
                        ellipse.updatePosition();
                    // System.out.println(list);
                    oos.writeObject(list);
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

在客户端:

import java.awt.geom.Ellipse2D;
import java.io.*;
import java.net.Socket;
import java.util.*;

public class TestClient {
    public static void main(String[] args) {
        try {
            List<TestEllipse> list = new ArrayList<TestEllipse>();
            TestEllipse t = new TestEllipse(50, 250);
            Socket socket = new Socket("localhost", 31362);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            while (true) {
                try {
                    oos.writeObject(t);
                    t = null;
                    list = (List<TestEllipse>) ois.readObject();
                    // System.out.println(list);
                    Thread.sleep(500);
                } catch (ClassNotFoundException | InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static class TestEllipse extends Ellipse2D.Double {
        int x, y;

        public TestEllipse(int x, int y) {
            super(x,y,10,10);
            this.x = x;
            this.y = y;
        }

        public void updatePosition() {
            x += 10;
        }

        @Override
        public String toString() {
            return "TestEllipse[x="+x+",y="+y+"]";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果将同一个对象多次写入给定的ObjectOutputStream,则该流会将该对象写入一次,然后在随后的时间写入对象的引用。这就是允许将对象的循环图发送到ObjectOutputStream的原因。

要确保发送更新的对象而不是引用,您需要在流上调用reset()