Networks coursework · Durham University
Multi-client chat & file server over TCP
BScNetworkingPythonSocketsTCPMultithreading
A multi-threaded client–server application built on raw TCP sockets: clients connect with a username, message the server, send private messages to each other, and use a command protocol for listing online users and downloading shared files.
What it does
The server listens on a port given on the command line, and clients connect with a username, host and port. It uses only Python's standard library: socket, threading and logging.
- One thread per client, so many users can be online at once.
- A framing protocol: every message is preceded by a fixed-size length header, so the receiver always knows exactly how many bytes belong to the next message.
- Private messages with
#username: message, routed by the server to the named client. - Commands for listing online users (
/online), browsing the server's shared folder (/show_files) and downloading a file (/download_file), plus/helpand/exit. - Server-side logging of connections and events.
Connection and message flow
- 01ConnectClient opens a TCP socket and sends its username
- 02Thread per clientServer spawns a handler thread and logs the connection
- 03Length-prefixed messagesFixed-size header, then the payload
- 04RouteTo the server, to another user (#name), or to a command
- 05RespondAcknowledgement, private delivery, user list or file bytes
Looking back
Revisiting the code with what I know now, I'd restructure the command dispatch (some branches ended up nested under the exit handler), stream files in chunks rather than a single 4 KB read, and wrap the connection in TLS: this version sends everything, including messages, in plain text.