[Java] Server is only receiving one movement packet

Started by
3 comments, last by Dave Hunt 8 years, 7 months ago

Hey guys!

So, My client and server are both in Java. The server is only receiving one player movement packet from the client and then can't receive any other packets.

Code:


Server reading a movement packet:
case Movement:
	float receivedX = packetStr.readFloat();
	float receivedY = packetStr.readFloat();
				
	for (GameClient s : connectedPlayers) {
		if (s.PlayerSocket.equals(client)) {
			s.X += receivedX;
			s.Y += receivedY;
			System.out.println("X:"+s.X + " - Y:"+s.Y);
			return;
		}
	}
	break;

Client sending position every frame update:
public void tick() {
	try {
		BasePacket movePacket = (BasePacket) new Packet3Movement(this.velX, this.velY);
		PacketWriter w = new PacketWriter(server, movePacket);
		w.sendPacket();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

The server is only printing the position once and then not printing it at all. When I try sending a disconnect packet, the server doesn't receive that after receiving the movement packet. Any help?

Advertisement

Where does that return go? Can you show the entire function for the server? My guess is the "return" causes your server to leave its receive loop, so it can't receive anything after it prints once and then returns.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Where does that return go? Can you show the entire function for the server? My guess is the "return" causes your server to leave its receive loop, so it can't receive anything after it prints once and then returns.

I tried removing the "return" because it looked weird to me too, but it still does the same thing it was doing.

Update:

I fixed it. All I had to do was add a check to see if the client is connected like so:


private void readExistingClient() throws Exception {
    // Read from connected clients.
    for (GameClient c : connectedPlayers) {
    while (c.PlayerSocket.isConnected()) { // This line here.
	  DataInputStream clientPacket = new DataInputStream(c.PlayerSocket.getInputStream());
	  readPacket(c.PlayerSocket, clientPacket);
	  }
     }
}

Unless the client opens a new connection every tick (a very bad idea) you're going to sit in that while loop until the client exits. You may need to rethink your design.

This topic is closed to new replies.

Advertisement