Network Programming in Java - #6 - Chat System #1 - Cleaning Test Files and Sending Usernames

Submitted by: 
Language: 
Visitors have accessed this post 186 times.

Introduction:
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.

Previous:
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.

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 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...

-From Client-

  1. con.name = "Test Client";

-From Server-

  1. 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-

  1. 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"...

  1. String loggedInUsername = System.getProperty("user.name");

Then do the same as before and set the packet variable "name" to loggedInUsername...

  1. con.name = loggedInUsername;

Finally send the Packet as before...

  1. client.sendTCP(con);

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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.