2015-11-16 54 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('')' –

相关问题