Java-Gaming.org Hi !
Featured games (88)
games approved by the League of Dukes
Games in Showcase (681)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (745)
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 955 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  
 
 
You cannot reply to this message, because it is very, very old.

 
CopyableCougar4 (61 views)
2016-06-25 16:56:52

Hydroque (93 views)
2016-06-22 02:17:53

SwampChicken (96 views)
2016-06-20 13:22:57

SwampChicken (99 views)
2016-06-20 13:22:49

SwampChicken (91 views)
2016-06-20 13:22:26

Hydroque (137 views)
2016-06-15 08:22:50

Hydroque (132 views)
2016-06-13 06:40:55

DarkCart (245 views)
2016-05-29 02:30:33

Hydroque (212 views)
2016-05-26 14:45:46

Mac70 (196 views)
2016-05-24 21:16:33
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17
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!