Skip to main content

Network Programming in Java - #8 - Chat System #3 - The Message Packet

This tutorial is the eighth in my Java Network Programming using KryoNet series, or third in creating a chat client and server system, in which we are going to be handling sending and receiving messages.

In the previous tutorial we created a client handler for our server to keep track of the connected clients.

The System:
We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat.
When a client connects, add them to a list.
Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation.

This Tutorial:
This tutorial we are going to be creating a Packet2Message class which will allow the client and server to send and receive messages.

The Packet:
In your client Java Project, Src Folder, Packets Package create a new Class named "Packet2Message". Again, we are going to extend Packet just like Packet1Connect does then we will create a message String variable...

  1. package Packets;
  3. public class Packet2Message extends Packet{
  4. public String message;
  5. }

Copy the Packet2Message class over to your server Java Project, Src Folder, Packets Package.

We also need to register the class in both the server and client by using the register method of KryoNet...

  1. server.getKryo().register(Packet2Message.class);


  1. client.getKryo().register(Packet2Message.class);

-Import the class in both projects-

Client Message Sending:
Now we are going to let the user of the client send a message. For now we are only going to let them enter it through the console by using a scanner, we will implement a GUI later on...

-Under the connecting block, where we send the Packet1Connect packet through TCP of the Client-

First we create a new Scanner to get the console user input, import this from import java.util.Scanner;

  1. Scanner scanner = new Scanner(;

Then we create a while loop to run while the Client is connected to the server...

  1. while (client.isConnected()) {
  2. }

In the loop, we output a line to the console to ask the user for a message to send to the server.

Then we get the next entry to the console (the message from the user to the server) and store it in a new message String variable.

Finally we create a new Packet2Message named messagePacket, set the message variable within the packet to our entered message, and send the packet through the client TCP.

  1. while (client.isConnected()) {
  2. System.out.println("Enter a message to send to the server:");
  3. String message = scanner.nextLine();
  4. Packet2Message messagePacket = new Packet2Message();
  5. messagePacket.message = message;
  6. client.sendTCP(messagePacket);
  7. }

Receiving the Message Packet:
Now, on the server, we want to add a Packet2Message instanceof check on the listener...

  1. server.addListener(new Listener() {
  2. public void received(Connection connection, Object object) {
  3. if (object instanceof Packet) {
  4. if (object instanceof Packet1Connect) {
  5. Packet1Connect con = (Packet1Connect) object;
  6. System.out.println("[CONNECTED] " +;
  7. clientHandler.addClient(new CustomClient(, connection));
  8. }else if(object instanceof Packet2Message) {
  9. Packet2Message mes = (Packet2Message) object;
  10. System.out.println("[RECEIVED] [" + clientHandler.getClient(connection).getUsername() + "] " + mes.message);
  11. }
  12. }
  13. }
  14. });

So the above code checks if the packet is a Packet2Message, if it is it creates a new Packet2Message called mes and sets it to the object received casted to Packet2Message, and outputs the message "[RECEIVED] [" followed by the username of the client we get through our clients list in our clientHandler, followed by another closing bracket and the message value of the Packet2Message object.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.