CPSC 225, Spring 2012
Lab Net Draw
This is our first lab on networking. For this lab, you won't be working with the network directly (that is, by using "sockets"). Instead, you will work with a framework that I wrote for network games and similar multi-user networked applications. The network framework that is used in the program is discussed in Section 12.5. This lab will give you some experience with networked applications and several of the issues involved in writing them. Next week, you will work directly with the socket API.
The application is a multi-user drawing program. The users all connect to the same server. When one user draws something, that user's program sends a message to the server about the item that was drawn. The server forwards the message to all the other users' programs, which respond by adding the same item to their draw windows. The result is that everything drawn by any user also shows up in all the other users' drawings. This is a demonstration-level program; it only does a few things, and there are a few things it doesn't do well. However, it does give an idea of how networked applications work.
Begin by starting a lab10 project. Copy the entire folder /classes/cs225/netgame into the src folder of your project. (Alternatively, download the zip file netgame.zip and unzip it to get a copy of the netgame folder.)
This lab is due next week. It should be in your homework folder by the afternoon of Friday, April 13.
Try It
Before beginning work on the exercise, you should try out a completed version. Run the program NetDrawComplete.jar by entering the following commands:
cd /classes/cs225 java -jar NetDrawComplete.jar &
This will put up a small dialog box that will ask you to enter a host name. During the lab, I will have a server running on my computer, eck.hws.edu. Enter eck.hws.edu as the host name and click OK. A drawing window should appear. The window will show any activity by any user connected to the server. To see this effect, if no one else is doing anything, open a second drawing window by entering the command "java -jar NetDrawComplete.java" again. Anything you do in one window should be reflected in the other.
Write It
There is a lot of code in the netgame package. The subpackage netgame.common contains the netgame framework. You should not mess with any of that code or expect to understand it at this point. The subpackage netgame.draw contains the NetDraw application. You will only work on the file DrawPanel.java. To run the application, run the class NetDrawMain. If you run the program before doing any work, you will have a simple, working, non-networked drawing program. The "Tools" menu allows you to select a drawing tool. The default is "Draw Curve", which allows you to draw a curve by dragging the mouse. There are also 12 "stamps" in the menu. If you select one of the stamps from the "Tool" menu, you can add a stamp to the picture by clicking with the mouse. Finally, there is a command in the "Tool" menu for selecting the color of the curve.
Your job is to add network support to the program. There is actually not a lot to do. Look for the places in the program that are labeled with TODO.
First of all, when the program starts, it has to connect to a server. The connection is represented by an object of type DrawClient. There is already a variable named client for this, but it is never assigned a value. You need to create the DrawClient object and assign it to client. The DrawClient class is a nested class in DrawPanel. You can see that its constructor requires a host and an IP address as parameters, and that it can throw an IOException.
The port number used by the NetDraw application is 32001. You have to get the host from the user. The program can do this at the beginning of the constructor in the DrawPanel class (the first TODO in the program). You should ask the user to enter the host name or IP address of the server. Use the standard dialog method
String host = JOptionPane.showInputDialog(this,"Enter host name or IP");
Remember that if this method returns null, then the user has canceled. Otherwise, you can use the return value, along with port number 32001, to create the client object.
Next, you have to make the program send messages to the server. This is the purpose of the client object, which includes a method client.send(msg). The messages in this program are Strings. Just pass the string that you want to send as the parameter.
You need to send a message to the server whenever the user draws something. This is done in the methods drawLine() and drawStamp(). You should add message-sending commands to these methods (the second and third TODO in the program). Message formats are described in the comment at the top of DrawPanel.java. For example, the message
stamp 3 167 207
means that stamp number 3 was drawn at the point (x,y) = (167,207). And
line 200 200 50 270 103 273 99
means that a line was drawn with RGB color (200,200,5) from the point (270,103) to the point (273,99).
Finally, you have to respond to messages that are received from the server. This is the largest part of the programming (but still not a lot). Messages are received in the DrawClient class, in a method called messageReceived(). However, that method already does some processing and passes the essential message string to the method netMessageReceived(), at the bottom of the class (the final TODO). Read the comment on this method carefully, then write the code to implement the method. I suggest using a Scanner to parse the message. A Scanner can be created to read characters from a string simply by using the construtor new Scanner(string).
Warning: Don't make the mistake that I did at first, namely to call the existing drawStamp method to draw a stamp or the drawLine to draw a line. The problem is that doing so will send another method to the server -- something that you certainly don't want to do in response to a message from the server!
Share It
Now that you have the program finished, you should be able to use it to connect to a NetDraw server. It should work with the server that is running on eck.hws.edu during the lab, but you will want to be able to use it even when that server is not running. To do that, you will have to run your own server.
To run a server, just run the program ServerMain in the package netgame.draw. The server runs indefinitely, until you terminiate it.
To connect your program to the server, you will need to know the name or IP address of the server computer. The name "localhost" can be used on most any computer to mean that computer itself. So, if your run NetDraw client on the same computer as the NetDraw server, you can use localhost as the host name for the server. If you run two copies of the program at the same time, you can see the networking in action even on one machine.
Of course, it's more fun to run the program on two different computers with two different users. To do that, you do need the IP or host name for the server. On Mac OS and Linux, the command-line command ifconfig will print out information about the network configuration of a computer. The IP address will be in there somewhere. Look for something of the form 172.20.17.122 (four numbers separated by periods). An alternative is running the program ShowMyNetwork in the package netgame.draw; it also produces output that includes the IP address. In any case, once you know the IP address of the server, you can connect your program to the server from another computer.