The evolution of a Lan Chat App

The Punjabi Geek
Oct 22, 2017 · 4 min read

This holiday season I thought to complete a new project as I was free. So I started searching for project ideas in Core Java as I am proficient in using it. All that I found were ideas like hospital management, inventory system etc. Ideas that only involved storing data into a database and then retrieve from it whenever we demand by using CRUD queries. As I already knew how to do all that so these ideas didn’t seem that intriguing to me. So I started to think on my own. And voila, it occured to me that I can make a Lan chat application by using network programming in Java.

So I started building the project. I started reading about how to make a program listen on a port and send a message from a client to a server. The first program that I made included sending a text from a client to a server that was listening on some port and the server should read the message and then display whatever was sent by the client. Oracle docs helped me alot. Here is a link to learning socket programming in Java.

Now what I wanted to make was a chat application in which mutiple users can chat. So if a user would log in and send a message the message should be sent to every user who is already logged in. At first I wrote a lot of code that was not even required in the first place. But as I started to progress, I realised I can do away with writing much less code.

In order to send message to every user I needed to broadcast the message. I learnt how to broadcast a message by using UDP from here

Now in the initial phase of making this application I asked a user that if he or she would like to host a chat(become a server listening on a port) or join a chat(become a client). After a while, I realised I didn’t need all that. So I decided to make my application using this approach:

Every user that logs in would start listening on a port. Whenever any user will type a message and click on send then the message will be broadcast to the port on which all the active users(also the sender) are listening. In this way, the message would be sent to everyone, including the sender itself because even the sender had to display the message in its chat window.

After struggling with completing my application for around a month, I was able to complete this functionality and I decided to test this by connecting my laptop with the old PC that I had in my home from my school days( I bet alot of you have that).

Me everytime an error occured

After completing my application, it was now time to add some more functionality to it. So I decided it would be nice if users would be able to see who else is online. So I created a textbox that displays all the users who are online. I achieved this by using my knowledge of multithreading. Here is a screenshot of my final application

Source:My own computer

So after around a month and a half my desktop application was complete and I pushed my project to github. The project can be found here

Me after completing the project

If you liked the post then you can clap for it so that other people will get to see it as well. Feel free to follow me here on medium and connect with me on LinkedIn and Twitter.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade