Files
zigp2p/examples
2026-02-06 21:03:59 -03:00
..
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00
2026-02-06 21:03:59 -03:00

P2P File Sharing Examples

This directory contains comprehensive example applications demonstrating the complete P2P file sharing system. Each example is designed to showcase different aspects and use cases of the system.

Example Files

1. basic_file_share.zig - Simple File Sharing

Purpose: Basic peer-to-peer file transfer functionality

Features:

  • Simple file sharing between two peers
  • Basic error handling and logging
  • File hash verification
  • Connection management

Usage Scenarios:

  • Simple peer-to-peer file transfer
  • Testing basic connectivity
  • Learning the file sharing API

Key Demonstrations:

  • File loading and hashing
  • Network connection establishment
  • File data transmission
  • Basic error handling

2. file_downloader.zig - Advanced Download Client

Purpose: Comprehensive file download client with advanced features

Features:

  • Multiple concurrent downloads
  • Download queue management
  • Resume capability for interrupted downloads
  • Progress monitoring and statistics
  • Connection pooling
  • Integrity verification

Usage Scenarios:

  • Download large files from multiple sources
  • Resume interrupted downloads
  • Download multiple files concurrently
  • Monitor download progress and performance

Key Demonstrations:

  • Multi-threaded downloading
  • Progress tracking and speed calculation
  • Download statistics and reporting
  • Resume capability simulation
  • Integrity verification process

3. file_server.zig - File Sharing Server

Purpose: Advanced file server for serving files to multiple clients

Features:

  • Multi-client file serving
  • Upload queue management
  • Partial file request handling
  • File discovery and listing
  • Client connection management
  • Bandwidth control

Usage Scenarios:

  • Share files with multiple peers simultaneously
  • Serve large files with resume capability
  • Manage multiple simultaneous uploads
  • Provide file discovery services

Key Demonstrations:

  • Concurrent client handling
  • File upload processing
  • Client session management
  • File listing and discovery
  • Upload progress monitoring

4. network_discovery.zig - Peer Discovery System

Purpose: Network discovery and peer management

Features:

  • Broadcast discovery for finding nearby peers
  • Peer table management and maintenance
  • Network topology mapping
  • Discovery timeout and retry logic
  • Peer capabilities tracking
  • Dynamic peer management

Usage Scenarios:

  • Discover peers in local network
  • Maintain peer connections in dynamic networks
  • Build network topology maps
  • Handle peer churn and reconnection

Key Demonstrations:

  • Network discovery protocols
  • Peer table operations
  • Topology analysis
  • Discovery retry mechanisms
  • Peer health monitoring
  • Dynamic network membership

Purpose: Complete P2P file sharing application combining all components

Features:

  • Command-line interface with multiple commands
  • Complete peer-to-peer file sharing
  • Network discovery and peer management
  • Upload/download queue management
  • Real-time progress monitoring
  • Configuration management
  • Error handling and recovery
  • Performance statistics

Usage Scenarios:

  • Run as a full P2P file sharing client
  • Serve files while downloading from others
  • Join existing P2P networks
  • Monitor network activity and performance

Commands Available:

  • share <file1> [file2] ... - Share files with network
  • download <file1> [file2] ... - Download files from network
  • serve - Start full P2P server
  • discover - Discover peers in network
  • status - Show application status
  • help - Show help information

Key Demonstrations:

  • Complete application architecture
  • Multi-threaded operation
  • Command-line interface
  • Real-time status monitoring
  • Comprehensive error handling
  • Performance optimization

Architecture Overview

Each example builds upon the previous ones, demonstrating:

  1. Core Networking (basic_file_share.zig)

    • Basic connection handling
    • Simple data transfer
    • Error management
  2. Advanced File Operations (file_downloader.zig, file_server.zig)

    • Complex transfer management
    • Progress tracking
    • Queue handling
    • Multi-client support
  3. Network Layer (network_discovery.zig)

    • Peer discovery protocols
    • Network topology
    • Dynamic peer management
  4. Integration (complete_app.zig)

    • All components working together
    • User interface
    • Production-ready features

Building and Running

Each example can be compiled and run independently:

# Build a specific example
zig build-exe basic_file_share.zig
zig build-exe file_downloader.zig
zig build-exe file_server.zig
zig build-exe network_discovery.zig
zig build-exe complete_app.zig

# Run examples
./basic_file_share
./file_downloader
./file_server
./network_discovery
./complete_app serve
./complete_app share document.pdf
./complete_app download movie.mp3

Dependencies

All examples use the core P2P system modules:

  • src/core/ - Core networking components
  • src/files/ - File handling and transfer
  • src/network/ - Network discovery and peer management
  • src/protocol/ - Message protocol definitions

Key Learning Points

These examples demonstrate:

  1. Progressive Complexity: Start with basic concepts and build up to full applications
  2. Real-world Patterns: Industry-standard approaches to P2P networking
  3. Error Handling: Comprehensive error management and recovery
  4. Performance: Efficient multi-threading and resource management
  5. User Experience: Clean interfaces and informative feedback

Extending the Examples

These examples serve as a foundation for:

  • Adding new features (encryption, compression, etc.)
  • Implementing different discovery protocols
  • Adding user interfaces (GUI, web interface)
  • Extending file format support
  • Adding security features
  • Performance optimizations

Each example is designed to be educational and extensible, providing a solid foundation for building production P2P applications.