One of the most basic starters program for demonstrating the proper use of Sockets in Java.
The following code makes use of Swing components to illustrate the Client GUI side and the Server side plays the role in Console mode.
How it works?
- Firstly, you need to start the server.
- Then start Client, to get connected with the server.
- A thread for that particular Client is created onto the server, which is maintained by the Conversation class.
- Client can only start communication after setting the username.
- Once done, when the client sends message to server, he adds his own username to it, so other people chatting with him can identify from whom the message arrived.
- On the server end, whatever message coming from client are added onto the MessageQueue.
- Server has a MessageDispatcher whose job is to dispatch message to all other users from the MessageQueue
- When server echo’s the message, its being received by all the clients connected to the server, which makes it a concurrent chat server
- The current code server can accept upto 5 clients at once, because its being run in a for loop to iterate upto 5 times, run it for infinite times wherein the server will be able to accept more than 100 clients at once.
- This can also be said as Conference Chatting Application.
- Client’s can stop chatting by just pressing the logout button.
- Once client is logged out, his socket is also closed on server side.
- Client Side
- Server Side