Network Programming in Java - #5 - Packets & KryoNet Registering

Introduction: This tutorial is the fifth in my Java Network Programming using KryoNet series in which we are going to be starting on Packets. Previous: In the previous tutorial we created a listener on our server, and sending a test TCP data-stream from our client. What Are Packets? Packets are essentially temporary files of data which are used to transmit the data from one location to another - our server to client, or vice versa. Our Packets: First create a new Package in our Src folder of our Client Java Project named "Packets". In there, create two new classes: Packet Packet1Connect We are not going to use Packet, but we are creating it to use it as a type. Inside Packet1Connect, add the "extends Packet" to the initial class line so we can reference the class as a Packet type...
  1. package Packets;
  2. public class Packet1Connect extends Packet{
  3. public String name;
  4. }
We also create a public String name variable so we can send the users name along with the Packet1Connect Packet class to receive it on the other side, the server. Copy the entire Packet Package over to your Src folder of your Server project. Client Packet Sending: Next, we are going to send the connect packet to our server from our client. Although the KryoNet libraries take care of the connecting, we are creating our own connection Packet so we can handle it differently on our server. So first create the Packet1Connect object...
  1. Packet1Connect con = new Packet1Connect();
Also import the class by hovering your mouse over the errors, and selecting import. Or press Ctrl, Shift and O to import all. Next we set the name String variable in our Packet1Connect to a "Test Client" string. And send the packet through TCP...
  1. con.name = "Test Client";
  2. client.sendTCP(con);
Server Packet Receiving: From our listener we created in the previous tutorial, we can accept the Packet of data sent through the TCP of the client. The Packet is stored in the Object object variable/parameter of the received method. First we check if the object is an instanceof Packet - if it is either a Packet, or a class extending or inheriting Packet. Then we check if the object, which we now know is a Packet, is a Packet1Connect. If it is, we create a new Packet1Connect variable named con and set it equal to the object parameter casted to Packet1Connect (Converted to).
  1. if (object instanceof Packet) {
  2. if (object instanceof Packet1Connect) {
  3. Packet1Connect con = (Packet1Connect) object;
  4. System.out.println("Hi, newly connected " + con.name + "!");
  5. }
  6. }
Once we have the con object as Packet1Connect. We output to the console "Hi, newly connected " followed by the value of the variable "name" in the Packet1Connect - which was set to "Test Client" before sending it from the client - followed by an exclamation mark ("!"). Registering Classes: Finally, to send the classes over KryoNet, we need to register them. You must register every class used to send over the TCP or UDP of KryoNet, even if it is a Java class such as String[].class (String Array)... In the server...
  1. server.getKryo().register(Packet.class);
  2. server.getKryo().register(Packet1Connect.class);
And, in the client...
  1. client.getKryo().register(Packet.class);
  2. client.getKryo().register(Packet1Connect.class);

Add new comment