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:
- Your client and server both need to stay alive and not exit after each message sent.
- Both the client and the server need to print every message received from the other party.
- 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:
- Type your code and when you are ready to run the program, click on Run. The server code should start up automatically.
- Open another terminal by clicking on +
- Type the command
python3 /usercode/udp.py client
Note that it can beserver
in place ofclient
. - Enter the text in the client window and see the effect.
- If the program is not running to your satisfaction:
- Kill the running server program by typing the break sequence
ctrl+c
orcommand+c
in both of the terminal windows. - Change the code.
- Click on Run.
- Type command
python3 /usercode/udp.py server
andpython3 /usercode/udp.py client
in the first and second terminal window, respectively. Go back to step 4.
- Kill the running server program by typing the break sequence
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.