/Users/lyon/j4p/src/net/web/PortMapper.java

1    package net.web; 
2     
3    import java.io.*; 
4    import java.net.Socket; 
5    import java.net.URL; 
6     
7    public class PortMapper { 
8        public static void main(String args[]) { 
9     
10           new PortMapper(80); 
11       } 
12    
13       public PortMapper(int port) { 
14           net.proxy.Proxy.setSoeProxy(); 
15           try { 
16               java.net.ServerSocket ss 
17                       = new java.net.ServerSocket(port); 
18               while (true) { 
19                   new PortMappingService(ss.accept()); 
20               } 
21           } catch (IOException e) { 
22               e.printStackTrace(); 
23           } 
24       } 
25   } 
26    
27   class PortMappingService implements 
28           Runnable { 
29       private Socket clientsSocket; 
30       private BufferedReader socketsBufferedReader; 
31       private OutputStream socketsOutputStream; 
32    
33       PortMappingService(Socket _s) { 
34    
35           clientsSocket = _s; 
36           try { 
37               socketsBufferedReader = 
38                       new BufferedReader( 
39                               new InputStreamReader( 
40                                       clientsSocket.getInputStream())); 
41               socketsOutputStream = clientsSocket.getOutputStream(); 
42           } catch (IOException e) { 
43               e.printStackTrace(); 
44           } 
45           Thread t = new Thread(this); 
46           t.start(); 
47       } 
48    
49       public void run() { 
50           try { 
51               String line = 
52                       socketsBufferedReader.readLine(); 
53               java.util.StringTokenizer 
54                       st = 
55                       new java.util.StringTokenizer( 
56                               line); 
57               System.out.println(line); 
58               if (st.nextToken().equals("GET")) 
59                   getABinaryUrl(st); 
60    
61               socketsOutputStream.close(); 
62               clientsSocket.close(); 
63           } catch (Exception e) { 
64           } 
65       } 
66    
67       public void getABinaryUrl(java.util.StringTokenizer st) 
68               throws FileNotFoundException, 
69               IOException { 
70           String urlName 
71                   = st.nextToken(); 
72           urlName = "http:/" + urlName; 
73    
74           URL url = new URL(urlName); 
75           InputStream 
76                   urlIs = url.openStream(); 
77           byte b [] = new byte[1024]; 
78           int numberRead = -1; 
79           while ((numberRead = urlIs.read(b)) > 0) 
80               socketsOutputStream.write(b); 
81       } 
82   } 
83