Skip to content

alex-obada/HuffmanDemo

Repository files navigation

.NET 8.0 Platform: Windows/Linux

HuffmanDemo

Demo CLI project in .NET 8.0 implementing the Huffman algorithm.
It allows you to send or receive encoded messages or files via TCP using Huffman compression.
I learned about it in a Data Structures and Algorithms course and it felt wrong that there wasn't any implementation provided in that course. So I took the matter into my own hands and spiced it up a bit.

How It Works

  • Commands
    • send <target:port> -m <message>: send a text message
    • send <target:port> -f <file>: send file contents
    • listen port <port>: receive and decode messages
    • help: show usage
  • The -v flag enables additional stats and can appear anywhere in the command (except with help).

Performance

When transferring a 10 KB uncompressed lorem ipsum sample, the algorithm achieved a compression ratio of approximately 50%, effectively reducing the data size by half.
However, since the compressed output also includes the encoding table, small text samples experience significant overhead and in some cases the total compressed size can grow up to 300% of the original data.

Requirements

  • .NET 8.0 SDK

Build & Run

git clone https://github.com/alex-obada/HuffmanDemo.git
cd HuffmanDemo
dotnet run -- <args>

Usage Examples

dotnet run -- send 127.0.0.1:9999 -m "Hello world"
dotnet run -- send example.com:8080 -f data.txt
dotnet run -- send localhost:9000 -v -m "Hello"
dotnet run -- listen -p 9999
dotnet run -- help

Project Structure

  • Program.cs — entry point and orchestration.
  • CliParser.cs — argument parsing and validation.
  • Huffman.cs / HuffmanTypes.cs — encoding/decoding logic.
  • NetworkHelper.cs — networking code (TCP listener/client).
  • BitHelper.cs / IBinarySerializable.cs — supporting utilities for binary operations.

Security & Limits

  • File size limit set (default ~10 MB) to protect against huge data loads.
  • Directory‑check to prevent reading directories by mistake.
  • The listener method blocks indefinitely until a client connects (no timeout by default).
  • Error handling catches I/O and invalid data exceptions, printing error message to stderr.
  • Endpoint parsing validates port range 1–65535, although some ports may require administrative privileges or already be in use by other applications.