How to make a basic instant messaging program in pure Java

Ben Leggiero picture Ben Leggiero · Dec 17, 2011 · Viewed 64.7k times · Source

Reinstating this question with a bounty! I need an example that stays online, like a real instant messenger! It needs to always be ready to receive or send a message to an arbitrary address over an arbitrary port, using TCP. The program must not quit after sending/receiving a message.

Bounty goes to whoever can give the best example of a real, usable instant messenger.


Looking online, all resources I found are either useless tutorials, dead threads, dead tutorials, ancient examples, or tell the programmer to use external APIs. How can I create a basic instant messenger from the ground up, only using Java SE?

There must be a way to do this, and some sample code would be appreciated. It only needs to perform the simplest tasks: Check if a compatible client is online on another computer (IP will be provided by the user) and send a TCP packet to that client, which will receive and display its contents.

Answer

ziesemer picture ziesemer · Dec 17, 2011

When this question was first asked and answered back in 2011, it was simply "Looking online, all resources I found are either useless tutorials, dead threads, or tell the programmer to use external APIs.". The provided links below met the criteria at the time. Further discussion follows in the comments.

First few Google results for "java socket chat":

Or from "java 8 chat client":

Many, many results following in the search. Pick one that suits your needs. You can even modify the Google search to only show results from the past year, if you wish.