Mini Project Report
Mini Project Report
Mini Project Report
AIM:
To create a Peer-To-Peer File-Transfer-Protocol using Centralized Directory
Architecture.
PROJECT DESCRIPTION:
Peer-to-Peer(P2P) Technologies are being widely used for sharing the data between
the servers and the clients. One of the major technology for file sharing that is implemented
nowadays is the Peer-to-Peer File Sharing System using centralized directory architecture
which is also known as Napster style Peer-To-Peer File-Transfer-Protocol. The older
versions of the systems used to have a single server which stores the files in its directory that
are received from the clients. The major drawback of these systems was that if a new file has
been created in one of the peers, it must be transferred to the server before another peer can
access it, which delays the process of transfer from one peer to another. This can be
conquered using the Napster system which allows the peer to peer file transfer.
SYSTEM ARCHITECTURE:
Processor Type: Intel(R) Core(TM) i7-9750H
CPU Frequency: 2.60GHz
Memory: 16GB
1
SYSTEM REQUIREMENTS:
JDK and Java to be installed
Server to execute the program on multiple systems
DESIGN:
Entire project is designed using Java where we have used the concepts of
Socket Programming and Multi-threading. For establishing the connections
between the Server and the Clients, we have used TCP/IP protocol using the
sockets.
Server Functionalities:
Registry
Client:
As a client, the user specifies a file name with the indexing server using
"lookup". The indexing server returns a list of all other peers that hold the file.
The user can pick one such peer and the client then connects to this peer and
downloads the file.
2
As a server, the peer waits for requests from other peers and sends the
requested file when receiving a request. The Peers (i.e., Clients) here, act as both
the client and the server. This server is different from the central index server
which only indexes the files. But, the server functionality of the peer
can be used to download the files from its directory. The peer acts a client to
download the files from other peers into its directory.
TRADEOFFS:
Instead of using the Array List for indexing, we can make using of the
Data Structures. Even though Array List works fine in our case, but in case of
randomly searching the files, hashing techniques serves well.
ABOUT TECHNOLOGY:
A peer-to-peer network allows computer hardware and software to communicate
without the need for a server. Unlike client-server architecture, there is no central server for
processing requests in a P2P architecture. The peers directly interact with one another
without the requirement of a central server.
Now, when one peer makes a request, it is possible that multiple peers have a copy of
that requested object. Now the problem is how to get the IP addresses of all those peers. This
is decided by the underlying architecture supported by the P2P systems. By means of one of
these methods, the client peer can get to know about all the peers which have the requested
object/file and the file transfer takes place directly between these two peers.
3
CENTRALIZED DIRECTORY ARCHITECTURE:
It is somewhat similar to client-server architecture in the sense that it
maintains a huge central server to provide directory service.
All the peers inform this central server of their IP address and the files they
are making available for sharing.
The server queries the peers at regular intervals to make sure if the peers are
still connected or not.
So basically this server maintains a huge database regarding which file is
present at which IP addresses.
Working:
Now whenever a requesting peer comes in, it sends its query to the server.
Since the server has all the information of its peers, so it returns the IP
addresses of all the peers having the requested file to the peer.
Now the file transfer takes place between these two peers.
The first system which made use of this method was Napster, for the purpose
of Mp3 distribution.
4
The major problem with such an architecture is that there is a single point
of failure. If the server crashes, the whole P2P network crashes. Also, since all of
the processing is to be done by a single server so a huge amount of the database
has to be maintained and regularly updated.
CODE:
Server.java
import java.io.*;
import java.net.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.Scanner;
import java.lang.Runnable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.Integer;
@SuppressWarnings("unused")
@SuppressWarnings("resource")
// public static void main(String args[])
public Server() throws NumberFormatException, IOException {
5
System.out.println("Waiting for the Client to be connected ..");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
// serverSocket.close();
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
new ServerTestClass(socket, globalArray).start();
}
}
}
@SuppressWarnings("unchecked")
public void run() {
try {
InputStream is = socket.getInputStream();
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(is);
filesList = (ArrayList<FileInfo>) ois.readObject();
System.out.println("All the available files from the given directory have
been recieved to the Server!");
for (int i = 0; i < filesList.size(); i++) {
6
globalArray.add(filesList.get(i));
}
System.out.println(
"Total number of files available in the Server that are
received from all the connected clients: "
+ globalArray.size());
}
catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds exception");
} catch (IOException e) {
System.out.println("I/O exception");
} catch (ClassNotFoundException e) {
System.out.println("Class not found exception");
}
try {
str = (String) ois.readObject();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(ServerTestClass.class.getName()).log(Level.SEVERE,
null, ex);
}
try {
oos.writeObject(sendingPeers);
} catch (IOException ex) {
Logger.getLogger(ServerTestClass.class.getName()).log(Level.SEVERE,
null, ex);
}
7
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
try {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
8
System.out.print("Enter the directory that contain the files : ");
directoryPath = br.readLine();
al = new ArrayList();
oos.writeObject(arrList);
// System.out.println("The complete ArrayList :::"+arrList);
System.out.print(
"Enter the desired file name that you want to downloaded
from the list of the files available in the Server : ");
9
String fileNameToDownload = br.readLine();
oos.writeObject(fileNameToDownload);
clientAsServerOOS.writeObject(fileNamedwld);
int readBytes = (int) clientAsServerOIS.readObject();
@SuppressWarnings("resource")
BOS.flush();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
FileInfo.java
import java.io.Serializable;
@SuppressWarnings("serial")
ServerDownload.java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
}
*/
ServerDownload(int peerServerPort,String directoryPath) {
this.peerServerPort=peerServerPort;
this.directoryPath=directoryPath;
}
public void run(){
try {
dwldServerSocket = new ServerSocket(peerServerPort);
dwldSocket = dwldServerSocket.accept();
new ServerDownloadThread(dwldSocket,directoryPath).start();
}
catch (IOException ex) {
Logger.getLogger(ServerDownload.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
class ServerDownloadThread extends Thread
{
Socket dwldThreadSocket;
String directoryPath;
public ServerDownloadThread(Socket dwldThreadSocket,String directoryPath)
{
this.dwldThreadSocket=dwldThreadSocket;
this.directoryPath=directoryPath;
}
@SuppressWarnings({ "unused", "resource" })
public void run()
{
try
{
ObjectOutputStream objOS = new
ObjectOutputStream(dwldThreadSocket.getOutputStream());
13
ObjectInputStream objIS = new
ObjectInputStream(dwldThreadSocket.getInputStream());
objOS.writeObject((int)myFile.length());
objOS.flush();
objOS.write(byte_arr,0,byte_arr.length);
objOS.flush();
}
}
catch(Exception e)
{
}
}
}
MyMain.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
14
System.out.println("*** P2P FTP using Centralized Directory Architecture
***");
System.out.println("1. To run the Server");
System.out.println("2. To run the Client");
System.out.print("Please enter your Choice : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int choice = Integer.parseInt(br.readLine());
if (choice == 1) {
Server s = new Server();
} else if (choice == 2) {
Client c = new Client();
} else {
System.out.println("Your choice is incorrect");
}
}
}
OUTPUT SCREENSHOTS:
Start of Server
15
Start of Client
16
P2P File Transfer Input
17
POSSIBLE IMPROVEMENTS:
We can improve the performance using the Data Structures
Could develop an User Interface.
Port numbers can be eliminated.
RESULT:
Hence, we have successfully created and implemented Peer-To-Peer File-Tranfer-
Protocol using Centralized Directory Architecture.
18