2013-03-19 19 views
0

我正在用Java創建一個用於文件共享的p2p應用程序。每個對等節點將在我的機器上運行在不同的端口上,並偵聽請求。但我遇到的問題是當創建一個PeerNode的實例時,我的代碼運行到無限循環。以下是我的代碼PeerNode。這是我應該如何創建每個節點並讓他們監聽傳入的請求?Java對等網絡應用 - 作業

下面的代碼表示一個對等節點:

public class PeerNode 
{ 
    private int port; 
    private ArrayList<PeerNode> 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(); 
     } 

    } 
} 

而繼類創建的所有節點和連接它們:

public class MasterClientServer 
{ 
    public static void main(String [] args) 
    { 
     int count = 10; 
     ArrayList<PeerNode> arrayOfNodes = createNodes(count); 
    } 

    public static ArrayList<PeerNode> createNodes(int count) 
    { 
     System.out.println("Creating a network of "+ count + " nodes..."); 
     ArrayList<PeerNode> arrayOfNodes = new ArrayList<PeerNode>(); 

     for(int i =1 ; i<=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 = "<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY>Error 404: Page Not Found</BODY></HTML>"; 
     } 

     // 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"; 
    } 
} 
+0

請詳細說明無限循環。代碼中的哪裏是程序計數器循環? – uldall 2013-03-19 22:32:19

+0

它進入StartClientServer函數並進入while()循環,它不會離開那裏。並且只創建一個PeerNode。當我創建PeerNode時,我應該在「while循環」中放置「偵聽TCP連接」代碼和thread.start..etc,以便可以偵聽更多請求? – user1998719 2013-03-19 22:37:46

回答

4

PeerNode構造函數永遠不會返回,因爲它正忙於接受新的連接。因此,您在createNodes中的循環僅創建第一個PeerNode實例。您可以通過在新線程中調用startClientServer來解決此問題:

new Thread(new Runnable() { 
    public void run() { 
     startClientServer(port); 
    } 
}.start(); 
+0

好的。所以我應該把它放在PeerNode的構造函數中?所以我創建了一個新的線程,它將在一個端口上啓動客戶端服務器,並在startClientServer函數中創建一個新的線程來處理請求。我有點困惑......爲什麼在構造函數中創建線程? – user1998719 2013-03-19 22:43:30

+0

是的,您可以啓動線程來處理構造函數中的accept()或'startClientServer'方法本身。在你的情況下,你需要一個線程通過accept()和一個處理每個新連接上的通信來接受新連接。 嚴格地說,你並不需要每個連接都有一個新的線程,但是如果你不這樣做,那麼你一次只能處理一個連接。 – uldall 2013-03-19 22:49:07