[kryonet] Disconnecting?

Well after i connect to the server and start my game everything is fine but my client disconnects after some time! Why is it doing so?

Are you sending some sort of KeepAlive-packet? Im almost sure, kryonet should send them automatically, but im not this sure if kryonet does this in both directions.

You know we really can’t help unless you post code. We can’t magically diagnose a problem if we have no code to work off of. Your two sentence OP provides no technical details about what happened or how your program is set up.

He is just asking for suggestion. Maybe someone has encountered similar problem and might give him their experience.

client sided network


package net.main;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import net.packets.PlayerAddPacket;
import net.packets.PlayerRemovePacket;
import net.packets.PlayerXUpdatePacket;
import net.packets.PlayerYUpdatePacket;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

public class Network extends Listener {

	private String ip;
	private int port;
	public Client c;
	private Game game;

	public Network(String ip, int port,Game game) {
		this.port = port;
		this.ip = ip;
		this.game = game;
	}

	public void start() {
		c = new Client();
		c.addListener(this);
		Kryo kryo = c.getKryo();
		kryo.register(PlayerAddPacket.class);
		kryo.register(PlayerRemovePacket.class);
		kryo.register(PlayerXUpdatePacket.class);
		kryo.register(PlayerYUpdatePacket.class);
		c.start();
		try {
			c.connect(10000, ip, port);
		} catch (IOException e) {
			e.printStackTrace();
			reconect();
		} finally {
			System.out.println("Status : " + c.isConnected());
		}
	}
	
	public void sendKeepAlive(int time){
		c.setKeepAliveTCP(time);
	}
	
	public void checkConnection(){
		if(!c.isConnected()){
			try {
				start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void reconect() {
		try {
			c.reconnect();
		} catch (IOException e) {
			e.printStackTrace();
			String i = JOptionPane.showInputDialog(null, "Cannot connect to : " + ip + port + " will exit!", 1);
			System.exit(0);
		}
	}

	public void received(Connection c, Object o) {
		if (o instanceof PlayerAddPacket) {
			PlayerAddPacket p = (PlayerAddPacket) o;
			MPPlayer mmp = new MPPlayer(p.x, p.y, p.id, p.name);

			game.players.put(p.id, mmp);
		}
		if (o instanceof PlayerXUpdatePacket) {
			PlayerXUpdatePacket p = (PlayerXUpdatePacket) o;
			game.players.get(p.id).x = p.x;
		}
		if (o instanceof PlayerYUpdatePacket) {
			PlayerYUpdatePacket p = (PlayerYUpdatePacket) o;
			game.players.get(p.id).y = p.y;
		}
		if (o instanceof PlayerRemovePacket) {
			PlayerRemovePacket p = (PlayerRemovePacket) o;
			game.players.remove(p.id);
		}
		c.setKeepAliveTCP(120000);
	}
}


Server side of the code

package net.main;

import java.util.HashMap;
import java.util.Map;

import net.packets.PlayerAddPacket;
import net.packets.PlayerRemovePacket;
import net.packets.PlayerXUpdatePacket;
import net.packets.PlayerYUpdatePacket;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class Network extends Listener {

	private int port, maxP;
	public Server server;
	public Map<Integer, Player> players = new HashMap<Integer, Player>();

	public Network(Properties p) {
		port = p.port;
		maxP = p.maxP;
	}

	public void start() {
		server = new Server();
		server.addListener(this);
		ServerGame.logger.info("Server created!");
		Kryo kryo = server.getKryo();
		kryo.register(PlayerAddPacket.class);
		kryo.register(PlayerRemovePacket.class);
		kryo.register(PlayerXUpdatePacket.class);
		kryo.register(PlayerYUpdatePacket.class);
		ServerGame.logger.info("Registered classes!");
		ServerGame.logger.info("Starting server....");
		try {
			server.bind(port);
			server.start();
			ServerGame.logger.info("Done! Server is operational!");
		} catch (Exception e) {
			ServerGame.logger.severe("Something went wrong with server!");
			e.printStackTrace();
		}
	}

	public void connected(Connection c) {
		// if (server.getConnections().length < maxP) {
		// c.setKeepAliveTCP(0);
		// ServerGame.logger.info("We receved connection ID:" + c.getID() +
		// " IP:" + c.getRemoteAddressTCP());
		// ServerGame.logger.info("Disconected, full server!");
		// } else {
		ServerGame.logger.info("We recs connection ID:" + c.getID() + " IP:" + c.getRemoteAddressTCP());
		for (Player p : players.values()) {
			PlayerAddPacket pack = new PlayerAddPacket();
			pack.x = p.x;
			pack.y = p.y;
			pack.name = p.name;
			pack.id = p.id;
			c.sendTCP(pack);
		}
		c.setKeepAliveTCP(120000);
		// }
	}

	public void setKeepAlive(int time) {
		for (Connection c : server.getConnections()) {
			c.setKeepAliveTCP(time * 100);
		}
	}

	public void disconnected(Connection c) {
		ServerGame.logger.info("We were disconected from ID:" + c.getID() + " IP:" + c.getRemoteAddressTCP());
		PlayerRemovePacket p = new PlayerRemovePacket();
		p.id = players.get(c.getID()).id;
		server.sendToAllExceptTCP(c.getID(), p);
	}

	public void received(Connection c, Object o) {
		if (o instanceof PlayerAddPacket) {
			PlayerAddPacket p = (PlayerAddPacket) o;
			Player p2 = new Player();
			p2.x = p.x;
			p2.y = p.y;
			p2.id = p.id;
			p2.name = p.name;
			p2.c = c;
			players.put(c.getID(), p2);
			server.sendToAllExceptTCP(c.getID(), p);
			ServerGame.logger.info("A player joined the game: " + p2.name);
		}
		if (o instanceof PlayerXUpdatePacket) {
			PlayerXUpdatePacket p = (PlayerXUpdatePacket) o;
			if (players.get(c.getID()) != null) {
				players.get(c.getID()).x = p.x;
				server.sendToAllExceptTCP(c.getID(), p);
			}
		}
		if (o instanceof PlayerYUpdatePacket) {
			PlayerYUpdatePacket p = (PlayerYUpdatePacket) o;
			if (players.get(c.getID()) != null) {
				players.get(c.getID()).y = p.y;
				server.sendToAllExceptTCP(c.getID(), p);
			}
		}
		if (o instanceof PlayerRemovePacket) {
			PlayerRemovePacket p = (PlayerRemovePacket) o;
			players.remove(players.get(players.get(c.getID())));
			server.sendToAllExceptTCP(c.getID(), p);
		}
		c.setKeepAliveTCP(120000);
	}
}


Well how often do you send update packets? I found that when I update too much (like every frame) I get disconnected too because something cannot keep up. Try to throttle how much you send maybe?

Other than that the code looks okay I guess. I mean it should work :stuck_out_tongue:

I am updating as the changes happen and am not continually sending data. And i have tryed to limit it on 0.1 and it sends every 0.1 s x and y and on every change but not working still disconecting!
here is all of the code for server and client

Some times it doesn’t disconnect, i don’t even know what i did… xD