Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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 2004 times)
0 Members and 1 Guest are viewing this topic.
Offline jaid

Senior Newbie





« Posted 2011-11-21 00: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: 355
Projects: 3
Exp: 5 years


I'm the King!


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

Could you remove the quotes from around the code?

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

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #2 - Posted 2011-11-21 01: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!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-11-21 02: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 Devvie


Medals: 3



« Reply #4 - Posted 2011-11-21 06: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 06: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: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-11-21 09: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 13: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.

 

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

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (62 views)
2014-11-26 15:22:04

toopeicgaming1999 (55 views)
2014-11-26 15:20:36

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (27 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!