[SOLVED] Kryonet Packet Recieving Issues

I’m just having a play around with Kryonet at the moment, trying to learn how it works and all that. Here’s my issue:

Whenever my client sends a packet, the server never tends to pick it up

ServerNetworkListener.java:


public class ServerNetworkListener extends Listener {
	public void connected(Connection c) {
		System.out.println("SOMEONE CAME IN :D");
	}
	
	public void disconnected(Connection c) {
		System.out.println("SOMEONE LEFT D:");
	}
	
	public void received(Connection c, Object o) {
		System.out.println("hit");
		if (o instanceof Packet) {
			Packet p = (Packet) o;
			if (o instanceof Packet001LoginToServer) {
				String user = ((Packet001LoginToServer)p).username;
				System.out.println(user + " logged in!");
			}
		}
	}
}

ClientMain.java:


public class ClientMain {
	private Client client;
	
	public ClientMain() {
		client = new Client();
		client.start();
		
		client.addListener(new ClientNetworkListener());
		
		Kryo k = client.getKryo();
		k.register(Packet001LoginToServer.class);
		
		try {
			client.connect(5000, InetAddress.getLocalHost(), 1234);
			client.sendTCP(new Packet001LoginToServer("LOLOLO"));
		} catch (Exception e) {
			e.printStackTrace();
			client.stop();
		}
		
//		String s = (String)JOptionPane.showInputDialog("Enter your username");
	}
	
	public static void main(String args[]) {
		new ClientMain();
	}
}

Here’s the output from the console:


SOMEONE CAME IN :D
SOMEONE LEFT D:

The connection to the server is clearly working, as the server outputs the text it should have output when a client connects, but it never outputs ‘hit’ or the username as I have coded it to.

Have I misunderstood here? Or is this just a bug in my network listener?

Thanks in advanced :smiley:

would need to see where you instantiate the server class.

also krynet has some quirkyness.

dont forget to call client.start() and server.start() if you expect anything to work. :slight_smile:

(which you think might just be handled more seamlessly )

Here is the server code:


public class ServerMain {
	private Server server;
	
	public ServerMain() throws IOException {
		server = new Server();
		registerPackets();
		server.addListener(new ServerNetworkListener());
		server.bind(1234);
		server.start();
		
		server.sendToAllTCP(new Packet001LoginToServer("LOLO"));
	}
	
	private void registerPackets() {
		Kryo kryo = server.getKryo();
		kryo.register(Packet001LoginToServer.class);
	}
	
	public static void main(String args[]) {
		Log.set(Log.LEVEL_DEBUG);
		try {
			new ServerMain();
			Log.set(Log.LEVEL_DEBUG);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Are you following a tutorial on YouTube? If you have, I’ve seen his tutorials and they are a little outdated, the code doesn’t run correctly because of changes in the library.

Yes I am. I wasn’t aware they were outdated :frowning:

Could you point me in the direction of some tutorials that are more modern?

Well, I think xSauceCode has some examples, but they aren’t super in depth :frowning: I haven’t looked much into Kryonet, but he sort of started me off, so I’d recommend him!

Are your client and server packets not only the same, but separate classes?

Did you try registering the packets before adding the Listener? I don’t know if this will fix it, but I think i had this problem before, and that was one of the things I did and it worked.

So, for those of you interested, I found out the bug in the program.

The issue was, that I needed to give the Packet001LoginToServer class an empty constructor that took no parameters

EDIT

Another few key things to note that I have learnt from this is that the client will instantly quit if it isn’t manually given a game loop, so even using a

while (true) { }

statement, means that packets can be sent and received without it instantly quiting

Also, I seemed to find issue when using these following statements:


client.sendTCP(packet);
packet.sendTCP(packet);

A way to send packets across is using the connection parameter that is found in the listener like this:


public void connected(Connection c) {
	System.out.println(c.getRemoteAddressTCP() + " connected");
	c.sendTCP(new Packet001IdentificationRequest());``
}