Peer-to-Peer File Sharing in Java and Node.js (Napster like)

Eyosiyas Girma
2 min readJun 20, 2021

--

p2p with a centralized server

Peer to peer network allows computers to communicate directly without a need for centralized servers. A computer downloads or uploads a file directly from its peers instead of accessing it from the centralized servers. of course, there are a couple of architectures but I will focus my attention on p2p with a centralized directory as it is easier to implement.

How does it work?

On the higher level, there is a centralized directory server that holds information about a peer such as names of its associated files, owner name, IP address, and port. Each node can see files associated with other nodes and can start downloading if the owner of the file is live thus acting as both server and client. We use underlining communication protocols to transfer the files.

How did I implement it?

The directory server holds the address, port, list of metadata of files, and a unique id to identify the peer and has two request handlers.

Each node communicates with the directory server with HTTP requests for uploading files’ metadata and getting a list of files’ names.

A node will read a file and write it to the output stream of the socket when another node asks it to do so thus acting as a server, concurrently it could read from the input stream of its socket and write it to the file output stream making it a client.

Summery

P2P Demonstration

In this series of articles I briefly discussed how p2p files transfer works and I included sample codes demonstrating that. I haven’t gone through how socket works, how to serialize objects so that we can transfer them and I left out the UI part as well because that is out of the scope.

Full code can be found here

--

--

Responses (1)