Sunday, 25 February 2018

Multi-person chat application : Java Socket Programming and Multi-threading


Hello Buddies.


Core java has 2 great things that are generally ignored these days in day to day coding - Multi-threading and socket programming, by the coders like us.
Since these 2 are not encountered by most of us in projects, let's prepare a small app that can give us the hands on experience on the usage.

The concept:

2 persons or players 
1 server
Chat app that serves the clients in a round robin fashion


1. Let's begin with the server first.

To implement the server in the easiest manner possible I would be using java.net.ServerSocket
Something like this -

static ServerSocket ss = null;
...
      ss = new ServerSocket(6666);
...

Socket s = ss.accept();    
...
new DataInputStream(s.getInputStream());

So the plan is simple, create a socket, dedicate a port, read the input stream striking at the port.


2. Now the next step is to have a client prototype.

It would be using the same socket's port to create an output stream of data. Something like this -
...
Socket s = new Socket("localhost", 6666);
...
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
...
dout.writeUTF(playerInput);


So far, looks good.

Create a main() for server and one for the player. Run both, and you would see the message reaching the server.


Now, what if we are having multiple main() methods invoked for various players.

Do you think the app will run smoothly?

Well it won't!


Every main() has its own thread. As soon as that thread invokes socket connection via server socket's accept(), it acquires the lock over it, leading to other threads starving i.e. non availability of the resource.

So what can be done here?

As a good programmer, you can handle this scenario by identifying the risky code blocks and making them thread safe.

Use synchronized blocks, and run the threads for the server and all the players or clients interacting in a parallel fashion.

Have a look here .

I have put a sample project in this GIT repo which shows the generalized way of using multi-threading and socket programming, and avoids thread starvation.


Following is an extract from the readMe file of the same -


# simpleChatApplicationJava

Here's a brief console flow to understand how the chat application code works. Please note S, P1 and P2 denote the respective console screens for the server (or a common chat viewer), person1 and person2.



S:
<<<<<<<<<<<<<<< SERVER >>>>>>>>>>>>>>



P1:
<<<<<<<<<<<<<<< Player 1 >>>>>>>>>>>>>>

Hey player 1 Please enter your message for server - 



P2:
<<<<<<<<<<<<<<< Player 2 >>>>>>>>>>>>>>

Hey player 2 Please enter your message for server - 



S:
<<<<<<<<<<<<<<< SERVER >>>>>>>>>>>>>>

Player accepted!

Player accepted!



P1:
<<<<<<<<<<<<<<< Player 1 >>>>>>>>>>>>>>

Hey player 1 Please enter your message for server - 

Player1 here

Hey player 1 Please enter your message for server - 



S:
<<<<<<<<<<<<<<< SERVER >>>>>>>>>>>>>>

Player accepted!

Player accepted!

Player Input: Player1 here



P2:
<<<<<<<<<<<<<<< Player 2 >>>>>>>>>>>>>>

Hey player 2 Please enter your message for server - 

Player2 here

Hey player 2 Please enter your message for server - 



S:
<<<<<<<<<<<<<<< SERVER >>>>>>>>>>>>>>

Player accepted!

Player accepted!

Player Input: Player1 here

Player Input: Player2 here



P1:
<<<<<<<<<<<<<<< Player 1 >>>>>>>>>>>>>>

Hey player 1 Please enter your message for server - 

Player1 here

Hey player 1 Please enter your message for server - 

q

Player 1 no longer available!




S:
<<<<<<<<<<<<<<< SERVER >>>>>>>>>>>>>>

Player accepted!

Player accepted!

Player Input: Player1 here

Player Input: Player2 here

Player Input: q

ERROR: No input from player!




P2:
<<<<<<<<<<<<<<< Player 2 >>>>>>>>>>>>>>

Hey player 2 Please enter your message for server - 

Player2 here

Hey player 2 Please enter your message for server - 

q

Player 2 no longer available!




S:
<<<<<<<<<<<<<<< SERVER >>>>>>>>>>>>>>

Player accepted!

Player accepted!

Player Input: Player1 here

Player Input: Player2 here

Player Input: q

ERROR: No input from player!

Player Input: q

ERROR: No input from player!



When a person gives 'q' or 'Q' in the input, the connection breaks off.

You can use this basic functionality to incorporate more persons in the chat room or to create a backend for GUI based chat application.



Why don't you try playing around with this some more, and do let me know the challenges and experiences encountered!!

Happy coding!

Featured post

JAVA based project, that can be used to hit DB using JDBC, from WSO2 ESB

Hi Buddies, Here is a small project that will enable you to hit MySQL DB using WSO2 ESB - https://github.com/namitsharma99/customM...

Popular Posts