CS61A Lab 15: Client/Server Programming

Week 8A, 2012

Solutions here chatclient.py chatserver.py

CS61AChat

We have written a nice little client-server set of programs for doing instant messaging! You can find the code in the following 4 files:

Enter the following commands to copy the relevant files over (starting in your home directory).

mkdir lab15
cd lab15
cp ~cs61a/lib/lab/lab15/*.py .

To run the chat program, first start the server program on a computer:

# python3 chatserver.py

This will ask you to find out your IP address and will tell you the port the server is using (it's randomly selected every time the server starts).

Note: If you're doing this from home, you're probably connected to a local router, in which case it won't work unless you're doing "port forwarding" (if you don't know what this is, don't worry about it). To handle this, log onto one of the school servers and use the server's full name (eg. star.cs.berkeley.edu) for the IP address when connecting the client.

Then start a client!

# python3 chatclient.py

This will ask you for a username, the server's address, and the server's port. Afterwards you can use /help to see all of the possible commands for the client. After logging on, you will receive any messages that others have sent you after each command (so you have to hit enter to receive new messages).

Question 0

Both the client and the server use the Message class to send messages to each other. A Message has four components:

Take a look at the code and list all the kinds of message actions that a Message sent from either the client or server can have. Who sends what types of messages?

Question 1

Modify the server program to understand a new type of message, which will have action broadcast. The server program should handle messages with action broadcast by sending the message on to all clients it has connected to it.

Once you have modified the server, add a new /broadcast [message] command to the client, which sends a broadcast message to the server.

Question 2

Modify the server program to understand a new type of message, which will have action me. The server program should handle messages with action me by sending the message on to all clients it has connected to it. It should differ from broadcast in that instead of printing 'billy says: hi everyone', it should say 'billy loves chocolate' if you were to type in (as billy) '/me loves chocolate'. This will require client side modification.

Question 3

Modify the client program to have a blocked users list. The client should not see any messages from anyone on their blocked users list! Add three new commands for the client: