Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Building an Internet chat system

Multithreaded client/server chat -- the Java way

  • Print
  • Feedback
You may have seen one of the many Java-based chat systems that have popped up on the Web. After reading this article, you'll understand how they work -- and know how to build a simple chat system of your own.

This simple example of a client/server system is intended to demonstrate how to build applications using just the streams available in the standard API. The chat uses TCP/IP sockets to communicate, and can be embedded easily in a Web page. For reference, we provide a sidebar explaining Java network programming components that are relevant to this application. If you're still getting up to speed, take a look at the sidebar first. If you're already well-versed in Java, though, you can jump right in and simply refer to the sidebar for reference.

Building a chat client

We start with a simple graphical chat client. It takes two command-line parameters -- the server name and the port number to connect to. It makes a socket connection and then opens a window with a large output region and a small input region.




The ChatClient interface

After the user types text into the input region and hits Return, the text is transmitted to the server. The server echoes back everything that is sent by the client. The client displays everything received from the server in the output region. When multiple clients connect to one server, we have a simple chat system.

Class ChatClient

This class implements the chat client, as described. This involves setting up a basic user interface, handling user interaction, and receiving messages from the server.

  • Print
  • Feedback

Resources
  • Related books
  • TCP/IP & Related Protocols, Uyless Black, McGraw-Hill
  • Routing in the Internet, Christian Huitema, Prentice Hall
  • IPv6The New Internet Protocol, Christian Huitema, Prentice Hall
  • Applied Cryptography, Bruce Schneier, John Wiley & Sons