无法将更新的对象值从客户端发送到服务器,服务器读取不存在的值
Cannot send updated object values from a client to a server, server reads values that do not exist
我正在为一项学校作业制作在线游戏。两个用户连接到服务器并玩合作回合制游戏,他们试图击败敌人。当我第一次将 GameCharacter 对象从客户端发送到服务器时,它不保留更新的值,只保留第一次发送的初始值。分配还必须包括线程和观察者模式。
下面是 类 中给出问题的片段。这是从 1 个客户端到 1 个服务器的实例。
//Sending from the client to the server
synchronized public void propertyChange(PropertyChangeEvent evt)
{
if (evt.getNewValue() instanceof GameCharacter)
{
//First time the code is run, all values are initialized. Current hp is 350.
//Second time the code is run, several values are updated. Current hp is 100
GameCharacter test = (GameCharacter) evt.getNewValue();
try
{
this.player = test;
System.out.println(test.getCurrentHp() + " Is the characters current hp in Client");
dos.writeObject(test);
} catch (IOException ex)
{
JOptionPane.showMessageDialog (null, "Error sending character to server" );
}
}
}
//The server immediately after receiving the object
//This is defined in the constructor
private ObjectInputStream ois;
@Override
public void run()
{
Object superTempHolder = null;
while (true)
{
try
{
superTempHolder = ois.readObject();
} catch (IOException ex)
{
System.out.println("I don't know how to receive.");
fixIOException(ex);
} catch (ClassNotFoundException ex)
{
System.out.println("I don't know what that is.");
fixClassNotFoundException(ex);
}
//If statement added for testing purposes
if (superTempHolder instanceof GameCharacter)
{
GameCharacter tester = (GameCharacter) superTempHolder;
System.out.println(tester.getCurrentHp() + " Is the current hp of the character in InputListener");
}
listener.propertyChange(new PropertyChangeEvent(this, Integer.toString(this.id), null, superTempHolder));
}
}
来自客户端的输出
350 Is the characters current hp in Client
100 Is the characters current hp in Client
来自服务器的输出
350 Is the current hp of the character in InputListener
350 Is the current hp of the character in InputListener
这是此程序可以将 GameCharacter 从客户端发送到服务器的唯一方法。 GameCharacter 已完全序列化,我不确定它在第二次发送时从哪里提取 350,因为实际上客户端上只存在一个 GameCharacter 实例。
最终,问题是:它正确发送了对象,但没有发送更新的值,并且服务器以某种方式读取了不再存在的信息。
你应该看看 ObjectOutputStream 中的 reset
方法。
Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream
. The current point in the stream is marked as reset so the corresponding ObjectInputStream
will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.
您应该在再次写入同一个对象之前调用重置。
您还可以使用 ObjectInputStream
中的 writeUnshared
method. If you decide to use writeUnshared, you should look into readUnshared
方法。
我正在为一项学校作业制作在线游戏。两个用户连接到服务器并玩合作回合制游戏,他们试图击败敌人。当我第一次将 GameCharacter 对象从客户端发送到服务器时,它不保留更新的值,只保留第一次发送的初始值。分配还必须包括线程和观察者模式。
下面是 类 中给出问题的片段。这是从 1 个客户端到 1 个服务器的实例。
//Sending from the client to the server
synchronized public void propertyChange(PropertyChangeEvent evt)
{
if (evt.getNewValue() instanceof GameCharacter)
{
//First time the code is run, all values are initialized. Current hp is 350.
//Second time the code is run, several values are updated. Current hp is 100
GameCharacter test = (GameCharacter) evt.getNewValue();
try
{
this.player = test;
System.out.println(test.getCurrentHp() + " Is the characters current hp in Client");
dos.writeObject(test);
} catch (IOException ex)
{
JOptionPane.showMessageDialog (null, "Error sending character to server" );
}
}
}
//The server immediately after receiving the object
//This is defined in the constructor
private ObjectInputStream ois;
@Override
public void run()
{
Object superTempHolder = null;
while (true)
{
try
{
superTempHolder = ois.readObject();
} catch (IOException ex)
{
System.out.println("I don't know how to receive.");
fixIOException(ex);
} catch (ClassNotFoundException ex)
{
System.out.println("I don't know what that is.");
fixClassNotFoundException(ex);
}
//If statement added for testing purposes
if (superTempHolder instanceof GameCharacter)
{
GameCharacter tester = (GameCharacter) superTempHolder;
System.out.println(tester.getCurrentHp() + " Is the current hp of the character in InputListener");
}
listener.propertyChange(new PropertyChangeEvent(this, Integer.toString(this.id), null, superTempHolder));
}
}
来自客户端的输出
350 Is the characters current hp in Client
100 Is the characters current hp in Client
来自服务器的输出
350 Is the current hp of the character in InputListener
350 Is the current hp of the character in InputListener
这是此程序可以将 GameCharacter 从客户端发送到服务器的唯一方法。 GameCharacter 已完全序列化,我不确定它在第二次发送时从哪里提取 350,因为实际上客户端上只存在一个 GameCharacter 实例。
最终,问题是:它正确发送了对象,但没有发送更新的值,并且服务器以某种方式读取了不再存在的信息。
你应该看看 ObjectOutputStream 中的 reset
方法。
Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new
ObjectOutputStream
. The current point in the stream is marked as reset so the correspondingObjectInputStream
will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.
您应该在再次写入同一个对象之前调用重置。
您还可以使用 ObjectInputStream
中的 writeUnshared
method. If you decide to use writeUnshared, you should look into readUnshared
方法。