Project: Write a UDP Chat App!

Welcome to your UDP project in this course!

We'll cover the following

Instructions

Writing a chat app is not so different from the capitalization code we saw in the last lesson. The idea is very simple. The client sends a message to the server and the server should respond with one. Both messages should be taken as input from the user. We’ve given you some basic starter code for it.

Here are some other factors you would want to consider to write your app:

  1. Your client and server both need to stay alive and not exit after each message sent.
  2. Both the client and the server need to print every message received from the other party.
  3. The server should not be chatting with more than one client.
import argparse, socket

MAX_SIZE_BYTES = 65535 # Mazimum size of a UDP datagram

def server(port):
    pass
    # Your code goes here

def client(port):
    pass
    # Your code goes here

if __name__ == '__main__':
    funcs = {'client': client, 'server': server}
    parser = argparse.ArgumentParser(description='UDP client and server')
    parser.add_argument('functions', choices=funcs, help='client or server')
    parser.add_argument('-p', metavar='PORT', type=int, default=3000,
                        help='UDP port (default 3000)')
    args = parser.parse_args()
    function = funcs[args.functions]
    function(args.p)

Note that to run the code, you would need to follow these steps:

  1. Type your code and when you are ready to run the program, click on Run. The server code should start up automatically.
  2. Open another terminal by clicking on +
  3. Type the command python3 /usercode/udp.py client Note that it can be server in place of client.
  4. Enter the text in the client window and see the effect.
  5. If the program is not running to your satisfaction:
    1. Kill the running server program by typing the break sequence ctrl+c or command+c in both of the terminal windows.
    2. Change the code.
    3. Click on Run.
    4. Type command python3 /usercode/udp.py server and python3 /usercode/udp.py client in the first and second terminal window, respectively. Go back to step 4.

Every time you make a change to the code you must click run for the changes to take effect


In the next lesson, we’ll look at the solution to this project.

Get hands-on with 1400+ tech skills courses.