All projects
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 /help and /exit.
  • Server-side logging of connections and events.

Connection and message flow

  1. 01
    Connect
    Client opens a TCP socket and sends its username
  2. 02
    Thread per client
    Server spawns a handler thread and logs the connection
  3. 03
    Length-prefixed messages
    Fixed-size header, then the payload
  4. 04
    Route
    To the server, to another user (#name), or to a command
  5. 05
    Respond
    Acknowledgement, 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.

← Back to all projects
Multi-client chat & file server over TCP — Nichita Roscin