Extras: Networking
Chat Server
This example is intended to tie together a lot of concepts. In this example,
you will use Exceptions, Collections, IO concepts, Threads with synchronization,
and Networking concepts. Here are the modules to this application:
- ChatArea class (ChatArea.java)-
This class contains an array of Chat class
objects. The Chat class contains everything that is part of any Chat session.
Currently, the Chat class only contains a LinkedList
object which contains the messages that need to be sent to a particular client.
In the exercise described below, you will probably want add new things to
this class. The ChatArea has some key synchronized
functions:
- putString - this routine is called to place in new string into each
Chat object
- getStrings - this routine is called to get all of the strings for a
given client
- waitForString - this is called to wait for new strings
- setStopFlag - calling this routine should shut down the chat room to
everyone.
- ChatServer class (ChatServer.java)-
This class opens a serverSocket and waits
for new connections. When they come, a new Chat
class is created and a new ChatServerThread
is created to process the new connection.
- ChatServerThread class(ChatServerThread.java)
- This class also includes a ChatReader class. The incoming socket is broken
into 2 IO Streams and each stream is processed by a separate thread. The incoming
stream is processed by the ChatReader thread. The ChatReader thread reads
incoming data and calls putString to update the chatArea. The original class
becomes a thread and waits for new chatArea information. When it comes, the
data is shipped out to the eager clients.
- MyInput class (MyInput.java)-
One of the purposes of this class is to allow the main ChatServer to be terminated
by typing #### at the keyboard. The other purpose of this class is to provide
keyboard input for ChatClients.
- ChatClient class (ChatClient.java)-
This is the program that the Chat Client users use. This program connects
to the desired host and port number. The created socket is split into an outbound
stream and an inbound stream. Keyboard input is processed with the MyInput
class and sent to the outbound stream of the socket. A separate thread handles
the incoming stream and puts the data into a TextArea component.
A picture of the overall design can be seen in the following Image:
Making your ChatClient Into an Applet
If you want to make your Chat Client into an Applet, you might find the following
useful:
chatRoom.html
ChatClientApplet.java
This applet assumes that your server runs on gettysburg.
Make sure that the port number for your Server matches the port number found
in the chatRoom.html param statement. Also I would
highly recommend that you compile ChatClientApplet.java and all of the classes
it uses with JDK1.1.8. Current browsers don't support JDK1.2.2 unless
you install a large plugin. If you set your path to T:\faculty\chasselbach\jdk\bin,
you will get JDK1.1.8.
When you start up your chat server on gettysburg, use port
9xx0 for your port number where xx contains the last 2 digits of your gettysburg
login.
Datagram programming
- java.net.DatagramSocket - member functions receive, send, and close
- java.net.DatagramPacket - member functions getAddress, getPort, and getData
Point to Point Quote Server
Source: QuoteServer.java
one-liners.txt
Source: QuoteClient.java
Multi-cast Quote Server
Source: MulticastServer.java
Source: MulticastClient.java
- You can use the MulticastSocket class derived from the DatagramSocket class.
Our example uses a regular DatagramSocket with a group address.
- address = InetAddress.getByName("230.0.0.1")
Using an address like this on a datagram packet send results in a broadcast.
- A multicast group is specified by a class D IP address, those in the range 224.0.0.1 to 239.255.255.255, inclusive,
- Receiving a multicast packet requires the following call:
socket.joinGroup(address) where address is obtained as shown above.
- socket.leaveGroup(address) to stop receiving multicast packets.
Exercise
Modify the ChatServer and the ChatClient to add Chat Names. From a user's perspective,
he will be prompted for his Chat Name before his session starts. This name will
be sent across to the Chat Server. From this point on, messages showing up will
always have the sender's name associated with it. For example:
Suppose that I connect to the chat room and say my name is Clem. Previously,
Sam and George have also connected and entered their names. Clem's screen should
look something like:
Sam says: I like this chat room ... everyone is so friendly
George says: At least most of the time
You say: I agree
This same conversation shows up on George's screen as:
Sam says: I like this chat room ... everyone is so friendly
You say: At least most of the time
Clem says: I agree
By the way, if you are a chat room enthusiast and want to do more, feel free
to be creative as long as you meet the minimum requirement specified above.
Hints:
You can automatically send the first packet to the server with the user's name.
I would add a new variable to the Chat class to hold the client's name. You
might want to check for uniqueness of the name. If you find that name already
used, you might want to append a number make the new name unique. When you find
yourself changing a name to make it unique, I would send one message to the
user who sent the duplicate name indicating that his name was altered to make
it unique. You will probably have to add a new function to the ChatArea class
to do this.
Optional extra work:
Note that if a Client goes away, that we don't currently reuse the child thread
spot. You might want to rectify this deficiency.
Last Modified:
November 15, 2002 0:12 AM