Jump to content
  • Advertisement
Sign in to follow this  
Xolitude

[Java] Server is only receiving one movement packet

This topic is 1123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by Dave Hunt

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!