[Kryonet] Client Listener does not react

Hey,
I have started using the Kryonet lib and I have some trouble with the network communication.
I followed the kryonet instruction, but either the server does not send a package or the client does not receive anything.

Client:


if (!Connection.load())
			System.exit(0);

public class Connection {

	static Client client;
	
	static boolean load() {

		try {

			client = new Client();
			client.start();
			client.connect(5000, "localhost", 54555, 54777);

			Kryo kryo = client.getKryo();
			kryo.register(Request.class);
			kryo.register(Response.class);

			Request request = new Request();
			request.text = "Here is the request!";
			client.sendTCP(request);

			client.addListener(new Listener() {
				@SuppressWarnings("unused")
				public void received(Connection c, Object o) {
						Sys.alert("A", "B"); // never called!
					if (o instanceof Response) {
						Response response = (Response) o;
						Out.say(response.text);
					}
				}
			});

			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}

Server:

Server server = new Server();
			server.start();
			server.bind(54555, 54777);
			
			Kryo kryo = server.getKryo();
			kryo.register(Request.class);
			kryo.register(Response.class);

			server.addListener(new Listener() {
				public void received(Connection c, Object o) {
					if (o instanceof Request) {
						Request request = (Request) o;
						System.out.println(request.text); // Here is the request

						Response response = new Response();
						response.text = "Thanks!";
						System.out.println(c.sendTCP(response)) // 11;
					}
				}
			});

Could you remove the quotes from around the code?

Why are you suppressing the “unused” warning in the client listener?

Probably to suppress warning about the unused Connection object?

Try to add some System.out.println()s in the received method, BEFORE the instanceof. That will help you detect if you’re actually receiving anything at all.

That shouldn’t throw an “unused” warning. But his code is exactly like that on the Kryonet site, so it’s quite weird how it’s not working :S

You should name your class to something else than Connection, because at the moment the received method doesn’t override anything because Listener class doesn’t obviously have corresponding method for receiving your Connection objects :slight_smile: This is why suppressing warnings is bad if you don’t know what they mean.

Eclipse said that I should remove method reiceive() because “The method received(Connection c, Object o) from type new Listener(){} is never used locally”. Looks like the GC takes something away from me. I updated the code in my first post.

Ha! “is never used locally” means that that method is doesn’t override anything and will never be called :slight_smile:
However, that is quite weird since it looks like it is correct (according to the website and the javadoc).

Damn, thank you, that was right,
the name Connection is already used by a parameter class. :slight_smile:
Everything works fine now, thank you guys!