Network Programming in Java - #6 - Chat System #1 - Cleaning Test Files and Sending Usernames
This tutorial is the sixth in my Java Network Programming using KryoNet series in which we are going to be beginning our multi-client chat system. We are going to be running off the test server and client so make sure you have followed and understood the previous tutorials of this series.
In the previous tutorial we created an Package of Packets involving Packet and Packet1Connect. As well as receiving and handling the Packet on the server side.
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 we are going to be removing the parts we don't need from the test applications, as well as sending the logged in username of the connecting clients' systems.
The Un-needed Testing Code:
We no longer need all of the following snippets of code, you may remove, comment out or keep these parts of code...
- con.name = "Test Client";
- System.out.println("Hi, newly connected " + con.name + "!");
The New/Adjusted Code:
Once we receive a new connecting packet on the server, we simply output a CONNECTED message to the server, so we can easily see who's connecting straight from the server...
-Replace the removed code from above with this-
- System.out.println("[CONNECTED] " + con.name);
Now for the sending the logged in users' username when connecting from the client...
First create a String variable named loggedInUsername and set it to the system property of "user.name"...
Then do the same as before and set the packet variable "name" to loggedInUsername...
- con.name = loggedInUsername;
Finally send the Packet as before...