Simple Proxy Server (Java) – JAVA編程語言程序開發技術文章

雖然問題很多,而且處理簡直就可以說是“沒有處理”。但是,還是可以通過這個代理瀏覽網頁,並且統計有多少流量流出和流入。代碼如下。(這篇帖子就是同個這個proxy發的)。

 

1. 將serverSocket.close()改成if (serverSocket!=null) serverSocket.close();後,異常打印少瞭很多。沒有null pointer exception瞭。

 2. 將第一次從瀏覽器讀取的request的count進行判斷,如果<=0,就直接關掉socket;如果大於0,才進行其他處理。瀏覽網頁速度變快瞭。不知道為什麼。

view sourceprint?001 package network.proxy; 

002   

003 import java.io.*; 

004 import java.net.*; 

005 import java.util.*; 

006   

007 // internet server <–serverSocket–> proxy <–clientSocket–> client                                                                                                    

008   

009 public class MyProxy { 

010     public static void main(String[] args) { 

011         try { 

012             new Thread(new SimpleProxyServer(8989)).start(); 

013         } catch (IOException e) { 

014             e.printStackTrace(); 

015         } 

016     } 

017 } 

018   

019 class SimpleProxyServer implements Runnable { 

020     private ServerSocket listenSocket; 

021     public SimpleProxyServer(int port) throws IOException { 

022         this.listenSocket = new ServerSocket(port); 

023     } 

024   

025     public void run() { 

026         for (;;) { 

027             try { 

028                 Socket clientSocket = listenSocket.accept(); 

029                 System.out.println("Create a new Thread to handle this connection"); 

030                 new Thread(new ConnectionHandler(clientSocket)).start(); 

031             } catch (IOException e) { 

032                 e.printStackTrace(); 

033             } 

034         } 

035     } 

036 } 

037   

038   

039 class ProxyCounter { 

040     static int sendLen = 0; 

041     static int recvLen = 0; 

042   

043     public static void showStatistics() { 

044         System.out.println("sendLen = " + sendLen); 

045         System.out.println("recvLen = " + recvLen); 

046     } 

047 } 

048   

049 // must close sockets after a transaction                                                                                                                                

050 class ConnectionHandler extends ProxyCounter implements Runnable { 

051     private Socket clientSocket; 

052     private Socket serverSocket; 

053   

054     private static final int bufferlen = 8192; 

055   

056     public ConnectionHandler(Socket clientSocket) { 

057         this.clientSocket = clientSocket; 

058     } 

059     public void run() { 

060         // receive request from clientSocket,                                                                                                                            

061         //extract hostname,                                                                                                                                              

062         //create a serverSocket to communicate with the host                                                                                                             

063         // count the bytes sent and received                                                                                                                             

064         try { 

065             byte[] buffer = new byte[bufferlen]; 

066             int count = 0; 

067   

068             InputStream inFromClient = clientSocket.getInputStream(); 

069             count = inFromClient.read(buffer); 

070             String request = new String(buffer, 0, count); 

071             String host = extractHost(request); 

072             // create serverSocket                                                                                                                                       

073             Socket serverSocket = new Socket(host, 80); 

074             // forward request to internet host                                                                                                                          

075             OutputStream outToHost = serverSocket.getOutputStream(); 

076             outToHost.write(buffer, 0, count); 

077             outToHost.flush(); 

078             sendLen += count; 

079             showStatistics(); 

080             // forward response from internet host to client                                                                                                             

081             InputStream inFromHost = serverSocket.getInputStream(); 

082             OutputStream outToClient = clientSocket.getOutputStream(); 

083             while (true) { 

084                 count = inFromHost.read(buffer); 

085                 if (count < 0) 

086                     break; 

087                 outToClient.write(buffer, 0, count); 

088                 outToClient.flush(); 

089                 recvLen += count; 

090                 showStatistics(); 

091             } 

092         } catch (IOException e) { 

093             e.printStackTrace(); 

094         } finally { 

095             try { 

096                 clientSocket.close(); 

097                 serverSocket.close(); 

098             } catch (IOException e) { 

099                 e.printStackTrace(); 

100             } 

101         } 

102     } 

103   

104     private String extractHost(String request) { 

105         int start = request.indexOf("Host: ") + 6; 

106         int end = request.indexOf('\n', start); 

107         String host = request.substring(start, end – 1); 

108         return host; 

109     } 

110 }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *