查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.example.proxy;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.common.IoAcceptor;
25  import org.apache.mina.common.IoConnector;
26  import org.apache.mina.common.IoConnectorConfig;
27  import org.apache.mina.transport.socket.nio.SocketAcceptor;
28  import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
29  import org.apache.mina.transport.socket.nio.SocketConnector;
30  
31  /**
32   * (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy 
33   * using MINA. The proxy only logs all data passing through it. This is only 
34   * suitable for text based protocols since received data will be converted into 
35   * strings before being logged.
36   * <p>
37   * Start a proxy like this:<br/>
38   * <code>org.apache.mina.example.proxy.Main 12345 www.google.com 80</code><br/>
39   * and open <a href="http://localhost:12345">http://localhost:12345</a> in a
40   * browser window.
41   * </p>
42   * 
43   * @author The Apache Directory Project (mina-dev@directory.apache.org)
44   * @version $Rev$, $Date$
45   */
46  public class Main {
47  
48      public static void main(String[] args) throws Exception {
49          if (args.length != 3) {
50              System.out.println(Main.class.getName()
51                      + " <proxy-port> <server-hostname> <server-port>");
52              return;
53          }
54  
55          // Create TCP/IP acceptor.
56          IoAcceptor acceptor = new SocketAcceptor();
57          ((SocketAcceptorConfig) acceptor.getDefaultConfig())
58                  .setReuseAddress(true);
59  
60          // Create TCP/IP connector.
61          IoConnector connector = new SocketConnector();
62  
63          // Set connect timeout.
64          ((IoConnectorConfig) connector.getDefaultConfig())
65                  .setConnectTimeout(30);
66  
67          ClientToProxyIoHandler handler = new ClientToProxyIoHandler(
68                  new ServerToProxyIoHandler(), connector, new InetSocketAddress(
69                          args[1], Integer.parseInt(args[2])));
70  
71          // Start proxy.
72          acceptor
73                  .bind(new InetSocketAddress(Integer.parseInt(args[0])), handler);
74  
75          System.out.println("Listening on port " + Integer.parseInt(args[0]));
76      }
77  
78  }