2015-11-16 53 views
-1

我寫了一個服務器類,我的客戶端是我的瀏覽器。當我在瀏覽器中輸入localhost:8082時,打開了硬編碼網站www.mmix.cs.hm.edu。到現在爲止還挺好。 一個網站通常有多個頁面。無論我點擊其他鏈接,我的服務器只能檢索主頁www.mmix.cs.hm.edu/index.html。我希望能夠導航到這些其他頁面。任何人都可以看看我的代碼,並告訴我如何繼續?客戶端 - 服務器應用程序Java

public static void main(String args[]) { 
    String fromClient = "www.mmix.cs.hm.edu"; 

    try(ServerSocket welcomeSocket = new ServerSocket(8082)){ 
     System.out.println("Server started, waiting for clients..."); 
     while(true){ 
      StringBuilder htmlCode = new StringBuilder(); 
      try(Socket connectionSocket = welcomeSocket.accept(); 
        DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream()); 
        BufferedReader fromBrowser = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()))){ 
        try(InputStream url = new URL("http://"+fromClient+"/index.html").openStream(); 
          BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){ 
          for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){ 
           htmlCode.append(line); 
          } 
          String str = htmlCode.toString(); 
          toClient.writeBytes(str); 
           //toClient.write("\r\n"); 
        } 
      } 
     } 
    } 
    catch(IOException io){ 
     io.printStackTrace(); 
    } 
} 
+0

考慮使用''而不是'URL'來直接將客戶端連接中的數據傳遞到服務器。 '新插座(「www.mmix.cs.hm.edu」,80)'。這將是更容易,更乾淨。 –

回答

1

@ ObiWanKenobi-更改了您的代碼以提取URL部分。嘗試下面的代碼片段。請通過代碼片段中的評論。運行並確認字符串操作是否有效。謝謝。

public static void main(String args[]) { 
     String fromClient = "www.mmix.cs.hm.edu"; 

     try(ServerSocket welcomeSocket = new ServerSocket(8082)){ 
      System.out.println("Server started, waiting for clients..."); 
      while(true){ 
       StringBuilder htmlCode = new StringBuilder(); 
       try(Socket connectionSocket = welcomeSocket.accept(); 
         DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream()); 
         BufferedReader fromBrowser = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()))){ 

         String line1 = fromBrowser.readLine(); //Line 1 is of format: GET /index.html HTTP/1.1 
         String dynUrl = line1.substring(line1.indexOf(32)+1,line1.lastIndexOf(32)); //dynUrl is of format:/index.html 
         //Please note that the query string parameters not taken into account and the code may fail if the query string contains space character. 
         //Construct a new URL based on dynUrl 
         try(InputStream url = new URL("http://"+fromClient+dynUrl).openStream(); 
           BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){ 
           for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){ 
            htmlCode.append(line); 
           } 
           String str = htmlCode.toString(); 
           toClient.writeBytes(str); 
            //toClient.write("\r\n"); 
         } 
       } 
      } 
     } 
     catch(IOException io){ 
      io.printStackTrace(); 
     } 
    } 
+0

非常感謝你,這對我有用。但我有個問題;爲什麼indexOf(32)+1,lastIndexOf(32)?你如何看待這個? –

+1

@ Obi_Wan_Kenobi254'32'實際上是空格'「」'的字符ASCII碼。 Java在編譯時將'32'轉換爲'space',因爲'char'和'int'是可轉換的。國際海事組織,這將是更好的編寫代碼,如'line1.indexOf('')+ 1,line1.lastIndexOf('')' –

相關問題