我正在使用基于tcp协议的java构建分布式游戏。当我从一个客户端向另一个客户端发送多个数据包时,接收部分只会一次又一次地获取第一个数据包。是否有人可以提供任何提示?先谢谢。
以下是接收方的代码: 注意MazePacket是一个实现可序列化的类 MazePacket packetFromClient;
/* stream to write back to client */
ObjectOutputStream toClient = new ObjectOutputStream(socket.getOutputStream());
/* stream to read from client */
ObjectInputStream fromClient = new ObjectInputStream(socket.getInputStream());
while ((( packetFromClient = (MazePacket) fromClient.readObject()) != null) && listening)
{
if (packetFromClient.type == MazePacket.MOVE_FORWARD || packetFromClient.type == MazePacket.MOVE_BACKWARD ||
packetFromClient.type == MazePacket.TURN_LEFT || packetFromClient.type == MazePacket.TURN_RIGHT ||
packetFromClient.type == MazePacket.FIRE)
{
/* update your local clock */
int counter=0;
int size=packetFromClient.timestamp.length;
System.out.println(" I am receiving packet with the timestamp:"+Arrays.toString(packetFromClient.timestamp));
synchronized(Mazewar.class) {
for(int i=0; i<size;i++) {
Mazewar.VectorClock[i]=Math.max(Mazewar.VectorClock[i],packetFromClient.timestamp[i]);
counter=counter+packetFromClient.timestamp[i];
}
}
/* tag the packet and put in queue */
packetFromClient.tag=counter;
ActionSequencer.ActionList.add(packetFromClient);
}
/* wait for next packet */
continue;
}