Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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] Exception when casting received Object (Bug?)  (Read 433 times)
0 Members and 1 Guest are viewing this topic.
Offline Xkynar

Junior Newbie


Medals: 1



« Posted 2014-07-13 06:18:02 »

I have this small piece of code in my class which extends Listener:

1  
2  
3  
4  
5  
6  
@Override
public void received(Connection connection, Object object)
{
    Packet packet = (Packet) object;
    server.addPacket(new ClientPacket(connection.getID(), packet));
}


Whenever I receive an Object, I cast it to an Interface called Packet with a method handle() which every packet implement. I then add it to a ConcurrentLinkedQueue for future processing.

Still, after i spam a few keys which send UDP packets to the server, the following Exception is thrown:

1  
2  
3  
4  
5  
Exception in thread "Server" java.lang.ClassCastException: com.esotericsoftware.kryonet.FrameworkMessage$KeepAlive cannot be cast to com.xkynar.game.net.packet.Packet
at com.xkynar.game.net.ServerSocket.received(ServerSocket.java:70)
at com.esotericsoftware.kryonet.Server$1.received(Server.java:61) at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246)
at com.esotericsoftware.kryonet.Server.update(Server.java:208)
at com.esotericsoftware.kryonet.Server.run(Server.java:356) at java.lang.Thread.run(Unknown Source)


The exception occurs in the cast, that is:

1  
Packet packet = (Packet) object;


How can this be possible? What the hell is "FrameworkMessage$KeepAlive" to begin with? Why is it entering my received listener?
Is it my mistake or a bug?
Thank you.
Offline trollwarrior1
« Reply #1 - Posted 2014-07-13 06:23:05 »

I have never used kryonet, but the best guess would be that KeepAlive is a packet which kryonet sends without your knowledge to not close the connection after certain period of time, or something like that.

Assuming that Packet is an interface or class that you created, you should do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
@Override
public void received(Connection connection, Object object)
{
    System.out.println(object.getClass().getSimpleName()); // you will see that you receive packets you didn't send.

    if(object instanceof Packet) {
        Packet packet = (Packet) object;
        server.addPacket(new ClientPacket(connection.getID(), packet));
    }
}
Offline Xkynar

Junior Newbie


Medals: 1



« Reply #2 - Posted 2014-07-13 06:33:59 »

-_- I feel angry I didnt remember of that. Maybe Its my lack of experience with networking libraries. That solved it, thanks alot.
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!