Network Programming in Java - #5 - Packets & KryoNet Registering
Submitted by GeePee on Thursday, April 30, 2015 - 23:57.
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...
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...
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...
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).
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...
And, in the client...
- package Packets;
- public class Packet1Connect extends Packet{
- }
- Packet1Connect con = new Packet1Connect();
- con.name = "Test Client";
- client.sendTCP(con);
- if (object instanceof Packet) {
- if (object instanceof Packet1Connect) {
- Packet1Connect con = (Packet1Connect) object;
- }
- }
- server.getKryo().register(Packet.class);
- server.getKryo().register(Packet1Connect.class);
- client.getKryo().register(Packet.class);
- client.getKryo().register(Packet1Connect.class);
Add new comment
- 133 views