A console-based, decentralized file-sharing application written in Java. Each instance of the application acts as both a client and a server, enabling users to search and download files directly from other peers without a central server.
Think of it as your own private file-sharing network, perfect for learning network programming, multithreading, and socket communication.
- Overview
- Features
- Key Terms
- Technologies Used
- Prerequisites
- Installation
- Usage Guide
- Example Workflow
- How It Works
- Network Configuration
- Troubleshooting
- Contributing
- License
- Educational Value
This project demonstrates network programming in Java by building a fully functional peer-to-peer (P2P) file sharing system. Unlike centralized services, there is no single server — every peer can connect to others, search for available files, and download them.
The application uses:
- Multithreading to handle multiple simultaneous connections.
- TCP socket programming for communication.
- Binary streaming for reliable file transfers.
- Decentralized Architecture — No central server; all peers are equal.
- Dual Role Peers — Each instance is both client and server.
- Multithreaded Connection Handling — Uses thread pools for efficiency.
- File Search — Find files on connected peers with keyword matching.
- Direct File Transfer — Fast binary transfers over TCP.
- Simple CLI Interface — Intuitive text commands for interaction.
- Automatic Folder Setup — Creates
shared/anddownloads/if missing.
- Peer: A network participant acting as both client and server.
- P2P: Direct communication without a central server.
- Socket: Network endpoint for sending/receiving data.
- TCP: Reliable, ordered communication protocol.
- Thread Pool: Pre-instantiated threads for concurrent tasks.
- Binary Stream: Raw byte transfer for accurate file sharing.
- Java 11+
- Socket Programming (TCP)
- Multithreading (ExecutorService)
- File I/O Streams
- Command-line Interface Design
- Java Development Kit (JDK) 11 or higher
- Basic CLI knowledge
- Same network connection or port forwarding for cross-network use
git clone https://github.com/<your-username>/java-p2p-file-sharer.git
cd java-p2p-file-sharermkdir shared downloadsecho "Hello, P2P World!" > shared/hello.txtjavac Peer.javaRun a peer instance:
java Peer <port>Example:
java Peer 9001Commands:
| Command | Description | Example |
|---|---|---|
connect <host> <port> |
Connect to another peer. | connect 192.168.1.10 9001 |
search <keyword> |
Search for files. | search hello |
download <filename> |
Download a file. | download hello.txt |
exit |
Exit the app. | exit |
Terminal 1 (Peer 1):
java Peer 9001Terminal 2 (Peer 2):
java Peer 9002
connect localhost 9001
search hello
download hello.txtResult: hello.txt is downloaded to Peer 2's downloads/ folder.
- Server Thread: Listens for connections and responds to search/download requests.
- Client CLI: Accepts user commands and sends requests to peers.
- Multithreading: Handles each connection in its own thread.
- File Transfer: Sends file size first, then binary data stream.
- Local Network: Find IP using
ipconfig(Windows) orifconfig(macOS/Linux), then connect. - Firewall: Allow chosen port for Java.
- Port in use: Choose another port.
- Connection refused: Ensure peer is online and port is correct.
- File not found: Verify in
shared/folder.
Potential improvements:
- GUI interface
- File encryption
- Peer discovery
- Resume downloads
- File integrity checks
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as you see fit!
This project is perfect for learning:
- Network programming in Java
- Socket communication
- Multithreading and concurrent programming
- File I/O operations
- Command-line interface design
- Peer-to-peer architecture concepts
Happy file sharing! 🎉
If you found this project helpful, consider giving it a star ⭐ on GitHub!