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.
- Commands
send <target:port> -m <message>: send a text messagesend <target:port> -f <file>: send file contentslisten port <port>: receive and decode messageshelp: show usage
- The
-vflag enables additional stats and can appear anywhere in the command (except withhelp).
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.
- .NET 8.0 SDK
git clone https://github.com/alex-obada/HuffmanDemo.git
cd HuffmanDemo
dotnet run -- <args>
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
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.
- 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.