1

I am building distributed game using java based on tcp protocol.When I send multiple packets from one client to the other one, the receiving part only gets the very first packet again and again.Could anyone give any hint? Thanks beforehand.

Here is the code in receiving side: Note MazePacket is a class which implements serializable 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;
            }
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Perhaps you might care to show us some of your code? – Graham Savage Mar 22 '14 at 22:16
  • 1
    TCP doesn't have such a thing as packets, so your description can't possibly match your code. Post your code. – user253751 Mar 22 '14 at 22:18
  • Your reading loop is incorrect. The readObject() method only returns null if you sent a null. It doesn't return null at end of stream. You have to catch EOFException to detect that. Also you should test 'listening' before calling readObject(), not after it. The 'continue' does nothing useful here. What happens in the rest of the read loop, which you've omitted? – user207421 Mar 23 '14 at 00:27

0 Answers0