Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务器中对象的更改未反映在客户端中_Java_Sockets - Fatal编程技术网

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

Java 服务器中对象的更改未反映在客户端中,java,sockets,Java,Sockets,我的客户机类中有一个TestEllipse对象,它扩展了发送到服务器的Ellipse2D.Double。服务器调用其updatePosition方法,将其包装在ArrayList中,并将ArrayList发送给客户端更新位置向椭圆的x坐标添加10 奇怪的是,即使调用了updatePosition,当客户端在ArrayList中接收到椭圆时,其位置的x坐标似乎没有改变。更新的位置显示在服务器中 [TestEllipse[x=60,y=250]] [TestEllipse[x=70,y=250]]

我的客户机类中有一个
TestEllipse
对象,它扩展了发送到服务器的
Ellipse2D.Double
。服务器调用其
updatePosition
方法,将其包装在ArrayList中,并将ArrayList发送给客户端<代码>更新位置向椭圆的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();
        }
    }
}
导入java.awt.geom.Ellipse2D;
导入java.io.*;
导入java.net。*;
导入java.util.*;
公共类测试服务器{
公共静态void main(字符串[]args){
试一试{
列表=新的ArrayList();
ServerSocket侦听器=新的ServerSocket(31362);
Socket=listener.accept();
ObjectOutputStream oos=新的ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois=新的ObjectInputStream(socket.getInputStream());
while(true){
试一试{
TestClient.TestEllipse e=(TestClient.TestEllipse)ois.readObject();
如果(e!=null)
列表.添加(e);
for(TestClient.TestEllipse椭圆:列表)
椭圆。updatePosition();
//系统输出打印项次(列表);
oos.writeObject(列表);
}捕获(ClassNotFoundException ex){
例如printStackTrace();
}
}
}捕获(IOEX异常){
例如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+"]";
        }
    }
}
导入java.awt.geom.Ellipse2D;
导入java.io.*;
导入java.net.Socket;
导入java.util.*;
公共类测试客户端{
公共静态void main(字符串[]args){
试一试{
列表=新的ArrayList();
TestEllipse t=新的TestEllipse(50250);
套接字=新套接字(“localhost”,31362);
ObjectOutputStream oos=新的ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois=新的ObjectInputStream(socket.getInputStream());
while(true){
试一试{
oos.writeObject(t);
t=零;
list=(list)ois.readObject();
//系统输出打印项次(列表);
睡眠(500);
}catch(ClassNotFoundException | InterruptedException ex){
例如printStackTrace();
}
}
}捕获(IOEX异常){
例如printStackTrace();
}
}
公共静态类TestEllipse扩展了Ellipse2D.Double{
int x,y;
公共测试工具(整数x,整数y){
super(x,y,10,10);
这个.x=x;
这个。y=y;
}
public void updatePosition(){
x+=10;
}
@凌驾
公共字符串toString(){
返回“TestEllipse[x=“+x+”,y=“+y+”]”;
}
}
}

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


要确保发送更新的对象而不是引用,您需要调用流。

谢谢!这正是我要找的。