Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Kryonet] Client Listener does not react  (Read 1289 times)
0 Members and 1 Guest are viewing this topic.
Offline jaid

Senior Newbie





« Posted 2011-11-21 01:10:03 »

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:
1  
2  
if (!Connection.load())
         System.exit(0);

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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;
              }
            }
         });


Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2011-11-21 02:06:05 »

Could you remove the quotes from around the code?

Why are you suppressing the "unused" warning in the client listener?

Offline theagentd
« Reply #2 - Posted 2011-11-21 02:20:36 »

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.

Myomyomyo.
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #3 - Posted 2011-11-21 03:43:08 »

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

Offline Waterwolf

Junior Member


Medals: 3



« Reply #4 - Posted 2011-11-21 07:32:14 »

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 Smiley This is why suppressing warnings is bad if you don't know what they mean.
Offline jaid

Senior Newbie





« Reply #5 - Posted 2011-11-21 07:35:54 »

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.

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.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #6 - Posted 2011-11-21 10:45:06 »

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

Offline jaid

Senior Newbie





« Reply #7 - Posted 2011-11-21 14:04:54 »

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 Smiley This is why suppressing warnings is bad if you don't know what they mean.

Damn, thank you, that was right,
the name Connection is already used by a parameter class. Smiley
Everything works fine now, thank you guys!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (111 views)
2013-05-17 21:29:12

alaslipknot (120 views)
2013-05-16 21:24:48

gouessej (149 views)
2013-05-16 00:53:38

gouessej (143 views)
2013-05-16 00:17:58

theagentd (156 views)
2013-05-15 15:01:13

theagentd (140 views)
2013-05-15 15:00:54

StreetDoggy (184 views)
2013-05-14 15:56:26

kutucuk (208 views)
2013-05-12 17:10:36

kutucuk (208 views)
2013-05-12 15:36:09

UnluckyDevil (213 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.23 seconds with 21 queries.