Network Programming in Java - #7 - Chat System #2 - Client Handler

Submitted by: 
Visitors have accessed this post 170 times.

This tutorial is the seventh in my Java Network Programming using KryoNet series, or second in creating a chat client and server system, in which we are going to be setting up a way to handle clients on our server.

In the previous tutorial we altered snippets of code to convert them from our test server and clients to our beginning point of chat server and 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 clientHandler which will be able to add, remove and interact with clients that are connected to the server.

Before we can create a client handler, we want to create a new class which we can use to modify the already created KryoNet Client Type. By doing this, we can pair the Client with a username and more information about the connected chat client.

  1. import com.esotericsoftware.kryonet.Client;
  2. import com.esotericsoftware.kryonet.Connection;
  4. public class CustomClient extends Client{
  5. private String username;
  6. private int id;
  7. private Connection con;
  9. public CustomClient(String user, Connection connection) {
  10. this.username = user;
  11. this.con = connection;
  12. = connection.getID();
  13. System.out.println("[INFO] Created client.");
  14. }
  16. public String getUsername() {
  17. return this.username;
  18. }
  20. public int getID() {
  21. return;
  22. }
  24. public Connection getConnection() {
  25. return this.con;
  26. }
  27. }

As you can see, we have the necessary imports for the classes used within this class, then we have the variable to hold the information for each client; username, id and connection.

On the constructor which is ran once the class is created, we set the variable to the parameter values and output an information line to the console saying that the client was created.

Finally we have some simple methods to return the information stored in the client; getUsername, getID and getConnection.

Now we are ready to create a ClientHandler class. First we import all the classes needed, then we create an ArrayList of CustomClients (what I named my client class from above, store it in the same package as your classes from the previous tutorials, on the server side) which is going to hold all of the connected clients for us to interact with.

  1. import java.util.ArrayList;
  2. import com.esotericsoftware.kryonet.Client;
  3. import com.esotericsoftware.kryonet.Connection;
  5. public class ClientHandler {
  7. public ArrayList<CustomClient> clients;
  9. public ClientHandler() {
  10. this.clients = new ArrayList<CustomClient>();
  11. }
  13. public void addClient(CustomClient client) {
  14. if (client.getConnection().isConnected()) {
  15. this.clients.add(client);
  16. System.out.println("[CONFIRM] Client " + client.getUsername() + " added to clients list.");
  17. }else
  18. System.out.println("[INFO] Client " + client.getUsername() + " is disconnected.");
  19. }
  21. public CustomClient getClient(Connection con) {
  22. for (CustomClient c : this.clients) {
  23. if (c.getID() == con.getID())
  24. return c;
  25. }
  26. return null;
  27. }
  29. public void showConnected() {
  30. for (CustomClient c : this.clients) {
  31. System.out.println(c.getUsername());
  32. }
  33. }
  34. }

We create the clients ArrayList and then initiate it on the constructor to a new ArrayList of the CustomClient Class Type - which extends the KryoNet Client Class Type so it is still a normal Client.

Then we have a couple of methods, one for adding a client, and another for getting the client from a connection.

Lastly we also have a method to show all of the connected clients by simply outputting the return string of the method getUsername in our CustomClient Class Type to the console for each CustomClient within our clients ArrayList. Confused?

Adding Clients:
Next we have to create a new ClientHandler in our Main class to use when we want to interact with the clients. Put this just under your initial class line (public class Main {) for example...

  1. public class Main {
  2. static ClientHandler clientHandler = new ClientHandler();

Finally we need to add the clients that connect using our Packet1Connect Packet to the clientHandler list. So, where we handle the Packet1Connect in our Main class, on the received void of the listener, we cast the object that was received to Packet1Connect, then add the newClient using the ClientHandler addClient method, and pass it a new CustomClient using the information given from 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. }
  9. }
  10. }
  11. });

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.