Advertisement
heavenriver

PeerGNUTELLA.java

Dec 11th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class PeerGNUTELLA
  6.     {
  7.     public static void main(String[] args) throws Exception
  8.         {
  9.         switch(args[0])
  10.             {
  11.             case "client": search(); break;
  12.             case "server": download(); break;
  13.             default: break;
  14.             }
  15.         }
  16.    
  17.     /* client mode: searches for a file */
  18.     static void search()
  19.         {
  20.         /* connection to peer; the peer ID is saved in "String peerID" */
  21.         Socket clientSocket = new Socket("peerID", 4445);
  22.        
  23.         /* opening buffers */
  24.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  25.         BufferedReader inputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  26.         DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
  27.        
  28.         /* search */
  29.         in.readLine(); // the first line is the mode: "client" or "server", and should be skipped
  30.         String searchID = in.readLine();
  31.         byte[] data;
  32.         /* converts user, pwd, and searchID into a packet to be flooded into the network */
  33.         InetAddress networkIP = InetAddress.getByName("hostid");
  34.         /* now that the own IP is known, it is tweaked and .0 is added to the end instead of using the own IP address
  35.            this way, the addressee will be the whole nearby network, including all of the user's nearby peers */
  36.         DatagramSocket searchSocket = new DatagramSocket();
  37.         DatagramPacket packet = new DatagramPacket(data, data.length, networkIP, 4446);
  38.         searchSocket.send(packet);
  39.         /* now the packet is flooded into the network and the nearest neighbour that knows who has the file will respond with the IP address of the file owner */
  40.         /* then, with said IP, another TCP connection (like the one to the first peer) will be established to transfer the file */
  41.        
  42.         clientSocket.close();
  43.         }
  44.    
  45.     /* server mode: provides a file to the peer requesting it */
  46.     static void download()
  47.         {
  48.         /* opening connection */
  49.         ServerSocket serverSocket = new ServerSocket(4445);
  50.        
  51.         while(true)
  52.             {
  53.             Socket clientSocket = serverSocket.accept();
  54.            
  55.             /* opening buffers */
  56.             BufferedReader inputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  57.             DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
  58.            
  59.             String clientInput = inputStream.readLine();
  60.             /* processes the request sent by the client and packs the data into a "byte[] data" */
  61.             outputStream.writeBytes(data);
  62.            
  63.             clientSocket.close();
  64.             }
  65.        
  66.         serverSocket.close();
  67.         }
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement