title: Java Peer-to-Peer networking application - homework
body: <p>I'm creating a p2p application in Java for file sharing. Each peer node will be running on my machine on a different port and listen for a request. but the problem I'm running into is when an instance of <code>PeerNode</code> is created my code runs into an infinite loop. Following is my code for <code>PeerNode</code>. Is this how I should create each node and have them listen for incoming requests?</p>
  
  <p>Following code represents one peer node:</p>
  
  <pre><code>public class PeerNode
  {
      private int port;
      private ArrayList&lt;PeerNode&gt; contacts;
      PeerNode preNode;
      PeerNode postNode;
      private String directoryLocation = "";
  
      PeerNode(int port)
      {
          this.port = port;
          this.setDirectoryLocation( port+"");
          startClientServer( port );
      }
  
      private void sendRequest(String fileName, String host, int port) throws UnknownHostException, IOException
      {
          Socket socket = new Socket(host, port);//machine name, port number
          PrintWriter out = new PrintWriter( socket.getOutputStream(), true );
          out.println(fileName);
  
          out.close();
          socket.close();
  
      }
  
      private void startClientServer( int portNum )
      {
          try 
          {
              // Establish the listen socket.
              ServerSocket server = new ServerSocket( 0 );
              System.out.println("listening on port " + server.getLocalPort());
  
              while( true )
              {
                  // Listen for a TCP connection request.
                  Socket connection = server.accept();
  
                  // Construct an object to process the HTTP request message.
                  HttpRequestHandler request = new HttpRequestHandler( connection );
  
                  // Create a new thread to process the request.
                  Thread thread = new Thread(request);
  
                  // Start the thread.
                  thread.start();
  
                  System.out.println("Thread started for "+ portNum);
              }
  
          } 
          catch (Exception e) 
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>And following class creates all the nodes and connects them:</p>
  
  <pre><code>public class MasterClientServer 
  {
      public static void main( String [] args )
      {
          int count = 10;
          ArrayList&lt;PeerNode&gt; arrayOfNodes = createNodes( count );
      }
  
      public static ArrayList&lt;PeerNode&gt; createNodes( int count)
      {
          System.out.println("Creating a network of "+ count + " nodes...");
          ArrayList&lt; PeerNode &gt; arrayOfNodes = new ArrayList&lt;PeerNode&gt;();
  
          for( int i =1 ; i&lt;=count; i++)
          {
              arrayOfNodes.add( new PeerNode( 0 ) ); //providing 0, will take any free node
          }
          return arrayOfNodes;
      }
  }
  
  
  
  public class HttpRequestHandler implements Runnable 
  {
      final static String CRLF = "\r\n";
      Socket socket;
  
      public HttpRequestHandler(Socket socket) throws Exception 
      {
          this.socket = socket;
      }
  
      @Override
      public void run() 
      {
          try 
          {
              processRequest();
          } 
          catch (Exception e) 
          {
              System.out.println(e);
          }
  
      }   
  
      /*
       * Gets a request from another node. 
       * Sends the file to the node if available.
       */
      private void processRequest() throws Exception 
      {
          /*DataOutputStream os = new DataOutputStream(socket.getOutputStream());
  
          InputStream is = socket.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
  
          // Get the request line of the HTTP request message.
          String requestLine = br.readLine();
  
          // Extract the filename from the request line.
          // In Get request, the second token is the fie name
          String[] tokens = requestLine.split(" ");
          String fileName = tokens[1];
  
          // Prepend a "." so that file request is within the current directory.
          fileName = "." + fileName;
  
          // Open the requested file.
          FileInputStream fis = null;
          boolean fileExists = true;
  
          try 
          {
              fis = new FileInputStream(fileName);
          } 
          catch (FileNotFoundException e) 
          {
              fileExists = false;
          }
  
          // construct the response Message
          // Construct the response message.
          String statusLine = null;
          String contentTypeLine = null;
          String entityBody = null;
          if (fileExists) 
          {
              statusLine = "HTTP/1.1 200 OK" + CRLF;
              contentTypeLine = "Content-Type: " + contentType(fileName) + CRLF;
          } 
          else 
          {
              statusLine = "HTTP/1.1 404 Not Found" + CRLF;
              contentTypeLine = "Content-Type: text/html" + CRLF;
              entityBody = "&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;404 Not Found&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;Error 404: Page Not Found&lt;/BODY&gt;&lt;/HTML&gt;";
          }
  
          // Send the status line.
          os.writeBytes(statusLine);
  
          // Send the content type line.
          os.writeBytes(contentTypeLine);
  
          // Send a blank line to indicate the end of the header lines.
          os.writeBytes(CRLF);
  
          // Send the entity body.
          if (fileExists) {
              sendBytes(fis, os);
              fis.close();
          } else {
              os.writeBytes(entityBody);
          }
          // Close streams and socket.
          os.close();
          br.close();
          socket.close();
  
      }
  
      private static void sendBytes(FileInputStream fis, OutputStream os)
              throws Exception 
              {
          // Construct a 1K buffer to hold bytes on their way to the socket.
          byte[] buffer = new byte[1024];
          int bytes = 0;
  
          // Copy requested file into the socket's output stream.
          while ((bytes = fis.read(buffer)) != -1) {
              os.write(buffer, 0, bytes);
          }*/
      }
  
      private static String contentType(String fileName) 
      {
          if (fileName.endsWith(".htm") || fileName.endsWith(".html")) 
          {
              return "text/html";
          }
          if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) 
          {
              return "image/jpeg";
          }
          if (fileName.endsWith(".gif")) {
              return "image/gif";
          }
          if (fileName.endsWith(".ram") || fileName.endsWith(".ra")) 
          {
              return "audio/x-pn-realaudio";
          }
          return "application/octet-stream";
      }
   }
  </code></pre>