Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Building an Internet chat system

Multithreaded client/server chat -- the Java way

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (4)
Login
Forgot your account info?

i can't see code of : public void run () ...By Anonymous on November 16, 2009, 3:44 pmi can't see code of : public void run () .. so can you explain it for me?

Reply | Read entire comment

can you explain more about function this : public void run () .By Anonymous on November 16, 2009, 9:56 amcan you explain more about function this : public void run () ...

Reply | Read entire comment

amizadeBy Anonymous on October 6, 2009, 10:51 amcndcjnsc

Reply | Read entire comment

lookin for some java help By Anonymous on May 6, 2009, 9:53 amlookin for some java help

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
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