๐Ÿธ pepe:curious

building a peer file sharing multi client app: java nginx docker aws....

๐Ÿ”— PeerLink: A Peer-to-Peer File Sharing App in Java and Next.js

PeerLink is a lightweight, full-stack peer-to-peer (P2P) file sharing application that allows users to upload and share files directly between two machines using an invite code mechanism. Built with Java on the backend and Next.js on the frontend, this project dives deep into systems-level programming topics like Java Streams, multi-threading, socket communication, and HTTP protocol parsing.

๐Ÿš€ GitHub repo: [https://github.com/singhdevhub-lovepreet/PeerLink]


As a backend and systems enthusiast, I wanted a project that would allow me to explore three things in depth:

1. Java Streams

Building a file transfer mechanism gave me the perfect opportunity to use:

2. Java Threading + Socket Server

Peer-to-peer means real-time parallel interactions. This required:

3. HTTP Protocol Parsing (Minimalist HTTP Server)

Instead of relying on full-fledged frameworks, I implemented:

PeerLink emerged as a side project turned educational sandbox for these ideas.


๐Ÿงฑ Project Architecture

Tech Stack

Layer Technology
Frontend Next.js (React 18, App Router)
Backend Java 11 (Maven project)
Protocol HTTP over custom sockets
Data Flow File Streams, P2P port transfer
Runtime Local server on port 8080 (backend) and 3000 (frontend)

Project Structure

PeerLink/
โ”œโ”€โ”€ src/main/java/p2p
โ”‚   โ”œโ”€โ”€ App.java                # Main server entry
โ”‚   โ”œโ”€โ”€ controller/             # REST endpoints
โ”‚   โ”œโ”€โ”€ service/                # File serving logic
โ”‚   โ””โ”€โ”€ utils/                  # Helper classes (ports, streams)
โ”œโ”€โ”€ ui/                         # Next.js frontend
โ”‚   โ””โ”€โ”€ src/app, components/    # UI pages and components
โ”œโ”€โ”€ start.sh / start.bat        # Quickstart scripts
โ””โ”€โ”€ README.md

๐Ÿ› ๏ธ How It Works

File Upload

File Sharing (Invite Code)

File Download


๐Ÿงต Concurrency with Threaded File Servers

Each file transfer happens in its own thread:

new Thread(() -> {
    try (ServerSocket serverSocket = new ServerSocket(port)) {
        Socket client = serverSocket.accept();
        // Serve the file via OutputStream
    } catch (IOException e) {
        // handle errors
    }
}).start();

This allows:

Thread Safety


Architecture

Image description


๐ŸŒ Minimal HTTP Server (Custom Parser)

Instead of using Spring or Jetty, I manually parsed HTTP headers:

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while (!(line = reader.readLine()).isEmpty()) {
    // Parse headers manually
}

This low-level approach gave me:

Example response:

HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="myfile.txt"
Content-Length: 12345

Then the raw file bytes are streamed via OutputStream.


๐Ÿ–ฅ๏ธ Frontend: Next.js UI

The frontend is built using Next.js App Router with two core components:

It uses fetch() to communicate with the backend and supports file preview + download with progress.


๐Ÿ” Security Notes


๐Ÿ“ฆ Deployment

You can run PeerLink in several ways:


๐Ÿงช Future Improvements


๐Ÿ Final Thoughts

PeerLink taught me how to:

If you're a backend developer wanting to learn systems-level Java โ€” this project is a perfect playground. Peer-to-peer doesn't have to be complex. Sometimes, all you need is a good port and a clean stream.


๐Ÿ”— Try It Out

  1. Clone the repo
  2. Run ./start.sh
  3. Drop a file โžก Share the code โžก Receive the file

Let the peer-to-peer fun begin ๐Ÿš€