tcp server only receives the first packet

给你一囗甜甜゛ 提交于 2019-12-25 12:07:29

问题


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;
            }

来源:https://stackoverflow.com/questions/22584243/tcp-server-only-receives-the-first-packet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!