Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  [SOLVED] Kryonet Packet Recieving Issues  (Read 594 times)
0 Members and 1 Guest are viewing this topic.
Offline DazKins
« Posted 2013-12-20 18:13:40 »

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

1  
2  
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  Cheesy

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline jmguillemette
« Reply #1 - Posted 2013-12-20 19:44:21 »

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. Smiley

(which you think might just be handled more seamlessly )

-=Like a post.. give the author a medal!=-
Offline DazKins
« Reply #2 - Posted 2013-12-20 20:18:51 »

Here is the server code:
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  
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();
      }
   }
}

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-12-20 22:23:18 »

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.

Offline DazKins
« Reply #4 - Posted 2013-12-20 22:26:11 »

Yes I am. I wasn't aware they were outdated   Sad

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

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-12-20 23:43:50 »

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

Offline Ashedragon

Junior Member


Medals: 2
Projects: 1


The best person you could possibly be is yourself.


« Reply #6 - Posted 2013-12-21 00:48:59 »

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

Need an amateur composer for your project? I charge $3~ (depending on the length) a song, and need the practice. Check out examples here: https://soundcloud.com/literature-corner

Offline Rayexar

Junior Member


Medals: 2



« Reply #7 - Posted 2013-12-21 13:20:38 »

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.
Offline DazKins
« Reply #8 - Posted 2013-12-21 13:24:30 »

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
1  
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:
1  
2  
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:
1  
2  
3  
4  
public void connected(Connection c) {
   System.out.println(c.getRemoteAddressTCP() + " connected");
   c.sendTCP(new Packet001IdentificationRequest());[tt][/tt]
}

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
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.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00

Cero (60 views)
2014-07-16 00:42:17

Riven (57 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!