0% found this document useful (0 votes)
3 views22 pages

Centralized P2P File Sharing in Java

Uploaded by

J Boh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Centralized P2P File Sharing in Java

Uploaded by

J Boh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Contents

No table of contents entries found.

[Document title]

Joyre Bohanon
[Document subtitle]
[Company name] | [Company address]
Abstract
A centralized, or hybrid, peer-to-peer network architecture was implemented to facilitate file
sharing between peer systems and a central indexing server for machine and file location. This
implementation was written in Java and tested sharing 40 different image, video, and text files.
With the use of multithreading, the server was able to handle multiple requests from multiple
clients. Each machine in the system has the capability to run as a client and a server.

Design Approach

In Figure 1, a pictorial representation of the P2P architecture is displayed. The S-routers act as
central indexing systems enabling connected clients to share what resources (files) they have
available and their location information (host name and port numbers). The S-routers
connections were maintained only for the purpose of adding this information to the indexing
mechanism in the form of a routing table and sharing these routing tables between the S-
routers. The ability to share data between servers is indicative of the “client behavior in the
servers because servers are able to request information from other servers as well as receiving.
Each sub-network are considered clients but also behave as servers as they are able to request
data other client machines and send data. This two-way communication, although expensive in
time resources, is useful because it virtually eliminates server overload/bottlenecking.

Implementation Modules
[Link]
The purpose of the the P2P client was to allow for message/data transfer between client
machines in the network. The main method of the P2PClient1 implemented sockets in triplet.
The ServerSocket ss enabled the client machine to listen to connections from other client
machines and handle each connected client with the use of the [Link] class, which
will be further discussed in later sections. The Socket s and p2pclient sockets were
implemented to enable sending and receiving. Although this could have been handled using
only one socket, closing the connection, and starting a new connection. To avoid file corruption,
and inappropriate stream flushing, each socket had dedicated streams. The s socket was used
for receiving and fulfilling requests. The p2pclient socket was used to send requests to
machines that had the requested resource. Access to requested resources were provided by
the routing tables which were broadcast by the respective S-Routers. Once the S-router filled its
routing table and received the routing table from the connected S-Router, the connection
between the client machines and the S-Routers were disengaged from the interprocess
communication. Upon receipt of the routing tables, the client machines used the location
information to share files.
For resource sharing, each client is prompted to give a general file name (no path) for each file
the machine is making available to the network. These general file names were stored in the S-
Router’s routing table. This implementation was helpful to the requesting client because it is
unrealistic for a computer on a network to know the direct file paths of each client. Although it is
possible to include paths in the indexing server, this way allowed for simple implentation without
the necessity of importing more resources from Java’ standard template library. Resource
lookup required the traversal of the ArrayList in the routing table, which showed to be a slow
process. If this implementation were rewritten for more speed and efficiency, a HashMap would
provide a more efficient solution for lookup.
Conversion of the shared files was enabled by converting files made available in the client
thread to byte arrays and were sent as objects via Java’s InputStream, ObjectInputStream, and
ObjectOutputStreams, and OutputStream facilities from the [Link] class. Each byte array was
sent as an object and processed in the client thread class. After processing, the byte array was
written into a file and opened via Java’s File class.

[Link]
The server router is what allows the clients and peers to connect directly, even without direct
knowledge of the other’s IP address. This module tracks sockets for other connections with
Socket and ServerSocket objects. It also makes use of the SR_Thread module. When
communicating with another server router, the two use threads to exchange data to allow the
building of the P2P connection.

SR_Thread.java
This module represents a thread of communication between server routers. The constructor
requires a routing table in the form of a 2D array, socket, connection type, and an ArrayList of
available files. This class is used by the TCPServerRouter module so server routers can
exchange necessary information and facilitate the P2P connections between the clients.

[Link]
This module is used in communication between clients. It holds a socket from the server router
and the client, available file names as an ArrayList, their paths in an array called filepaths, and
tracks their destinations by using the routing table passed throughout the modules to locate the
host and the specific port needed to reach the other client successfully.

Technical Aspects
In a centralized P2P, n-client machines are connected to a Server Router that routes and
broadcasts the client machines’ locations and available resources for file sharing. The
architecture is considered centralized because the Server Routers provide indexing and
registration facilities for the participating client machines. Without such indexing, the client
machines would not be able to connect to each other and share files. This is beneficial because
the individual the client machines do not have to have previous knowledge of the location of files
in order to communicate. This particular implementation is scalable by increasing the size of the
routing tables in the Server Routers. However, this type of system has 2 clear points of failure in
that a fault in the server routers would halt all communication. If the failure occurs during client
machine connections before the routing tables are completed, there would be no way to recover
except to debug and relaunch the system. As such, this type of architecture is not
recommended for critical use unless there is round the clock maintenance.

Simulation
Outputs
The output shown here are screenshots of the output of only a few files to show an example of
how the final output presents.
Output Data
Image Files Output Summary:
File Size (kb) Transmission Rate (MBps)
1 0
5
25
155
301
761
2097

Video Files Output


File Size (kb) Transmission Rate (MBps)
1354 27
1722 18
2953 28
3581 27
27623 29
46392 32
94494 32

Text Files Output


File Size (kb) Transmission Rate (MBps)
10 27
18
2953 28
3581 27
27623 29
46392 32
94494 32
Summary

Conclusion

Appendix
P2P_Client1.java
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
public class P2P_Client1 {
public static void main(String[] args) throws IOException{
//int type_signal = 0;
Scanner scan = new Scanner([Link]);
Socket socket = null, p2psocket = null;
ServerSocket ss = null;
ObjectInputStream ois;
ObjectOutputStream oos;
OutputStream os;
InputStream is;
ArrayList<String> available_files = new ArrayList<>();
InetAddress address = [Link]();
String host = [Link](), filename = null;//response = null
Boolean requesting;
String destination_host = null;
int destination_port = 0;
Object[][] other_RTable = new Object[10][3];
//Object[][] RTable = new Object[10][3];
Boolean Running = false;
byte[] byte_arr = null;
int num_files, request = 0;
int port_num = 5555;
long startTime = 0;
long file_size = 0;

[Link]("How many files would you like to make available for


sharing?");
num_files = [Link]();
[Link]();
for(int i = 0; i < num_files; i++) {
[Link]("Enter file name " + i + 1 + ": ");
filename = [Link]();
available_files.add(filename);
}

//connecting to server router


try {
socket = new Socket(host, port_num); //the client socket
}catch(UnknownHostException e) {
[Link](host + " does not exist");
[Link](1);
}catch (IOException e) {
[Link]("Couldn't get I/O for the connection to: " + host);
[Link](1);
}
os = [Link]();
oos = new ObjectOutputStream(os);
try {
[Link](available_files);
[Link]();
}catch (IOException e) {
[Link]();
}

[Link]();

[Link]("Connected to ServerRouter.");

//Accepting Connections from other clients


ss= new ServerSocket(0);

while(Running) {
try {
p2psocket = [Link]();
[Link]("Server socket ss is listening on port: " +
[Link]());

}catch(IOException e) {
[Link]("Could not listen on port " + [Link]());
[Link](1);
}
}

[Link]();

//sending/receiving files from other clients


[Link]("Are you requesting file (enter 1 for yes or 2 for no)?");
request = [Link]();

if(request == 2) {

requesting = false;
}else if(request == 1) {
requesting = true;
[Link]("Enter the file name (without path):");
filename = [Link]();
startTime = [Link]();
[Link](filename);
[Link]();

try {
is = [Link]();
ois = new ObjectInputStream(is);
try {
other_RTable = (Object[][]) [Link]();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
[Link]();
}
[Link]();
[Link]();
ClientThread ct = new ClientThread(p2psocket, requesting,
filename , other_RTable, destination_host,
destination_port, byte_arr);
[Link]();
is = [Link]();
ois = new ObjectInputStream(is);
try {
byte_arr = (byte[]) [Link]();
File file = new File("\"C:\\Users\\joyre\\P2P file output\"");
file_size = [Link]();
try (FileOutputStream fos = new FileOutputStream(file)) {
[Link](byte_arr);
[Link]().open(file);
[Link]();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
[Link]();
}

long endTime = [Link]();


[Link]("Operation Time: " + (endTime - startTime) + " ");
[Link]("Transfer rate: " + ((file_size/(endTime-
startTime))/1000) + "MBps");

[Link]();

}catch(IOException e) {
[Link]();
}

}
[Link]();
}
}

TCPServerRouter
import [Link].*;
import [Link];
import [Link].*;
public class TCP_ServerRouter {

public static void main(String[] args) throws IOException{


// type_signal = 1;
ServerSocket ss = null; //this socket is for accepting all other connections
Socket socket = null; //this socket is for communicating with others sr's and
clients
Object[][] routing_table = new Object[10][3]; //column 0 = socket column 1 =
socket column 2 = files in socket
ArrayList<String> available_files;
InputStream is;
ObjectInputStream ois;
OutputStream os;
ObjectOutputStream oos;
int port_num = 5555;
String host = [Link]().getHostName();
Object[][] other_RTable = new Object[10][3];

Boolean Running = true;


int index = 0;

//Connection Acceptance
try {
ss = new ServerSocket(port_num);
[Link]("Server socket is listening on port: " + port_num);
}catch(IOException e) {
[Link]("Could not listen on port: " + port_num);
[Link](1);
}

//Creating threads with connections


while(Running == true) {

try {
socket = [Link]();
is = [Link]();
ois = new ObjectInputStream(is);

if([Link]().getHostName().equalsIgnoreCase("j263-
[Link]")) //we will have to get the hostname of the machine the other SR is on
{
try {
other_RTable = (Object[][]) [Link]();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
[Link]();
}
} else {
available_files = new ArrayList<String>();
try {
available_files = (ArrayList<String>)
[Link]();
} catch (ClassNotFoundException e) {
[Link]();
}
SR_Thread t = new SR_Thread(routing_table, socket,
index, 1,available_files);
[Link]();
index++;
[Link]("ServerRouter connected with
Client/Server: " + [Link]().getHostAddress());

}catch (IOException e) {
[Link]("Client/ServerRouter failed to connect.");
[Link](1);
}
}

Socket server_router = new Socket(host, 5556);

//sending routing table to other server router


try {
os = server_router.getOutputStream();
oos = new ObjectOutputStream(os);
[Link](routing_table);
}catch (IOException e) {
[Link]();
}

[Link]();
server_router.close();

}
}
SR_Thread.java
import [Link].*;
import [Link].*;
import [Link].*;
public class SR_Thread extends Thread{
private Object[][] RTable;;
private ArrayList<String> available_files;
private Socket client_socket, sr_socket, p2p;
private String file_request = null;
private int connection_type, index;
Boolean requesting;
SR_Thread(Object[][] RTable, Socket s, int index, int connection_type,
ArrayList<String>available_files)throws IOException{
[Link] = index;
RTable[index][0] = [Link]();
RTable[index][1] = s;
RTable[index][2] = available_files;
this.connection_type = connection_type;
this.available_files = available_files;
client_socket = s;
}

SR_Thread(Object[][] other_RTable, Socket socket, Boolean requesting){


this.client_socket = socket;
[Link] = requesting;
}

public void run() {


try {
[Link]();
[Link](10000);
}catch(InterruptedException e) {
[Link]("Thread Interrupted");
}

[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class ClientThread extends Thread{

private Object[][] other_RTable;;


private ArrayList<Object> available_files;
private Socket client_socket, sr_socket;
private String file_request = null;
private int connection_type, index;
private Boolean requesting;
private String destination_host = null;
private int destination_port = 0;
byte[] byte_arr;
final String[] filepaths = {"C:\\Users\\joyre\\missiontrip.mp4", "C:\\Users\\joyre\\
puppyvideo.mp4", "C:\\Users\\joyre\\skateboardtricks.mp4",
"C:\\Users\\joyre\\[Link]", "C:\\Users\\joyre\\[Link]", "C:\\
Users\\joyre\\[Link]",
"C:\\Users\\joyre\\[Link]","C:\\Users\\joyre\\
[Link]", "C:\\Users\\joyre\\[Link]",
"C:\\Users\\joyre\\cookingsteak.mp4"};

public ClientThread(Socket s, Boolean requesting, String file_request, Object[][]


other_RTable,
String destination_host2, int destination_port2, byte[] byte_arr) {
this.client_socket = s;
[Link] = requesting;
this.file_request = file_request;
this.other_RTable = other_RTable;
this.destination_port = destination_port;
this.destination_host = destination_host;
this.byte_arr = byte_arr;
// TODO Auto-generated constructor stub
}

public void run() {


ArrayList<String> available_files;
Boolean found;
String file_names;
String fn = null;
byte[] byte_arr = null;
for(int i = 0; i < 10; i++) {
available_files = (ArrayList<String>)other_RTable[i][3];
for(int j = 0; j < 3;j++) {
file_names = available_files.get(j);
if(file_names.equalsIgnoreCase(file_request)) {
found = true;
}
}
destination_host = (String)other_RTable[i][0];

destination_port = (int)((Socket) other_RTable[i][1]).getPort();


found = false;
if(found == false) {
break;
}
}
if(requesting == true) {

if(file_request.equalsIgnoreCase("missiontrip.mp4")) {
fn = filepaths[0];
}else if(file_request.equalsIgnoreCase("puppyvideo.mp4")) {
fn = filepaths[1];
}else if(file_request.equalsIgnoreCase("skateboardtricks.mp4")) {
fn = filepaths[2];
}else if(file_request.equalsIgnoreCase("[Link]")) {
fn = filepaths[3];
}else if(file_request.equalsIgnoreCase("[Link]")) {
fn = filepaths[4];
}else if(file_request.equalsIgnoreCase("[Link]")) {
fn = filepaths[5];
}else if(file_request.equalsIgnoreCase("[Link]")) {
fn = filepaths[6];
}else
if(file_request.equalsIgnoreCase("[Link]")) {
fn = filepaths[7];
}else if(file_request.equalsIgnoreCase("[Link]")) {
fn = filepaths[8];
}else if(file_request.equalsIgnoreCase("cookingsteak.mp4")) {
fn = filepaths[9];
}
FileInputStream fileIn;
try
{
fileIn = new FileInputStream(fn);
byte[] arr = new byte[(int)[Link]()];
[Link](arr);
[Link]();
byte_arr = arr;

}catch(Exception e)
{
[Link]();
}
try {
OutputStream os = client_socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
[Link](byte_arr);
[Link]();
[Link]();

} catch (IOException e) {
// TODO Auto-generated catch block
[Link]();
}

}else if(requesting == false) {

}
}

You might also like