You type into the entry line at the bottom, hit Enter, and
everyone else in the chat room gets your message. The server
sends a message whenever anyone logs on.
.class
files. Your
job will be to replace the executables with your own, one by one.
This saves you from having to simultaneously debug a client and a
server. A reference for the given classes is available. You do
not have to use the same class structure, package structure, or
design as the given code.
field name | size/encoding | content |
---|---|---|
n |
byte |
length of name in bytes |
name |
byte[n] |
name of person sending message |
m |
int (4 bytes, network byte order) |
length of text in bytes |
text |
byte[m] |
text of message |
(The fields are sent top first, bottom last.)
The idea behind this standard is that everyone's chat room should end up being compatible. That is, every client should work with every server.
The protocol is stateless, and clients are not expected to be able to show messages that were sent before they connected to the server. The server will discard messages once they are sent, and there is no way to get them back.
The client and server don't exchange any information other
than these chat messages. That is, the client doesn't need to
send any requests to the server, and the server will send no
acknowledgment. You can tell when the connection has failed
because you'll get an IOException
or something like that
the next time you try to read or write a message. The client
needs to watch for this in case the server goes down, and the
server needs to watch for this because clients are allowed to
disconnect at any time.
Since this is a networking course, we've given you the source for the GUI for free in the file SimpleGUI.java (included with the assignment download). You are free to use or modify it, or not to use it at all. Please include it with your final submission if you use it.
The sample solution and the GUI source code can be downloaded
here. The code is already built, but
you may rebuild it by typing javac *.java
. To
test the chat code, type
java ThreadedServer
on one host and
java SClient Nickname servername
on another. Both hosts may be the same, in which case you should
use localhost
for the hostname, or just leave it
blank.
ServerSocket
and Socket
classes in package java.net
.
These classes use TCP/IP streams. When it receives a message from
a client, it should send a copy of it to all its clients
(including the one it came from). Clients should be able to
connect to and disconnect from the server at will. When a client
connects, the server should broadcast a message from "server"
stating that someone has logged in, and give their host and IP
address. (There is no way to give their name at this point. That
comes in a later assignment on authentication.)
Note that the client really needs to be multi-threaded, but Java happens to do most of the work for you. One thread waits for messages to come down the network connection, while the other thread handles window events, key strokes, and sending messages. In Java, the AWT stuff automatically runs in its own thread, so you can use the main thread to do all your reading.
The host and port where the clients look for the server should not be hard coded. It's okay to put in a default value, but it should be possible to change the server host and port either with a command line option, a system property, a dialog box, a configuration file, something like that. Be sure to document how this works in your README file.
The clients and server must be able to run on any machine and any port.
ServerSocket
. When it detects a new client, it
should do the following:
Socket
from the
ServerSocket
, and get the associated input and
output streams.