采用老的socket(诸塞式),通过代理访问socket server,如下代码可以实现:
public class Client 
{
static Socket server; public static void main(String[] args) throws Exception {

//设置代理.     
String proxyHost = "192.168.0.76";
String proxyPort = "1080";
System.getProperties().put("socksProxySet","true");
System.getProperties().put("socksProxyHost",proxyHost);
System.getProperties().put("socksProxyPort",proxyPort); 
String host = "192.168.0.76";
int port = 5678; System.out.println("connetioning:" + host + ":" + port);
server = new Socket(host, port); PrintWriter out = new PrintWriter(server.getOutputStream());

String str = "test\r\n";
out.println(str);
out.flush();
System.out.println("finish send message");

Thread.sleep(1000);

BufferedReader in = new BufferedReader(new InputStreamReader(server
.getInputStream()));
String resp = in.readLine();
System.out.println(resp);
server.close();
}
}
但是在nio socket中,传统的socket设置代理的方法不能用,如下代码不能通过代理连接server:
String proxyHost = "192.168.0.76";
String proxyPort = "1080";
System.getProperties().put("socksProxySet", "true");
System.getProperties().put("socksProxyHost", proxyHost);
System.getProperties().put("socksProxyPort", proxyPort);    try
    {
        SocketChannel client = SocketChannel.open();
        InetSocketAddress isa = new InetSocketAddress(host, port);
        client.connect(isa);
        client.configureBlocking(false);
    }
    catch (UnknownHostException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
200分求在nio中如何设置socket代理?

解决方案 »

  1.   

    nio无阻塞io,如果要用代理,必须该代理也支持nio
    不然的话就没有意义,普通的socks代理不能代理nio连接
      

  2.   

    我们的代理服务器采用ccproxy,是不是这个的问题?
      

  3.   

    我写的一个程序,用到了代理,希望对你有帮助。
    public class SimpleFrame extends JFrame {
        private JPanel contendPanel = new JPanel();    private JPanel norPanel = new JPanel();    private JTextArea jtx = new JTextArea();    private JTextField jtf = new JTextField();    private JButton btn = new JButton("Connect");    public SimpleFrame() {
            contendPanel.setLayout(new BorderLayout());
            norPanel.setLayout(new GridLayout());
            norPanel.add(jtf);
            norPanel.add(btn);
            contendPanel.add(jtx);
            contendPanel.add(norPanel, BorderLayout.NORTH);        jtf.setText("http://www.baidu.com");        this.getContentPane().add(contendPanel);
            this.setSize(400, 300);
            this.setVisible(true);
        }    public void conn() {
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    StringBuffer document = new StringBuffer();                try {
                        InetAddress addr = InetAddress.getByName("proxyall.neusoft.com");
                        InetSocketAddress sa = new InetSocketAddress(addr.getHostAddress(), 8080);
                        // 通过代理服务器连接
                        Proxy proxy = new Proxy(Proxy.Type.HTTP, sa);
                        Authenticator.setDefault(new Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication("username", new String("password").toCharArray());
                            }
                        });
                        URL url = new URL(jtf.getText());
                        URLConnection conn = url.openConnection(proxy);                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));                    String line = null;
                        while ((line = reader.readLine()) != null) {
                            document.append(line + "\n");
                        }
                        System.out.println(document);
                        jtx.setText(document.toString());
                        reader.close();
                    } catch (MalformedURLException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
        }    public static void main(String[] args) {
            SimpleFrame simobj = new SimpleFrame();
            simobj.conn();
        }
    }
      

  4.   

    我刚刚看了一下java的API文档,有很多疑惑 
    java.net.Proxy这个类构造函数是 
    Proxy(Proxy.Type type, SocketAddress sa) 可以这样建立连接: 
    Socket socket = new Socket(new Proxy(......)); 
    socket.connect(serverAddress); 
    但这让我有点疑惑,我没搞懂在nio中怎么用proxy,因为nio中不直接new Socket的,而是通过SocketChannel.open()这个静态方法创建一个socketChannel,它返回的socketChannel已经没有办法设置Proxy。因为socket除了构造函数可以用Proxy构造,并没有提供一个setProxy方法让我在open socketChannel后再进行Proxy的设置。可能它设想的是我在system property里设置好了代理,它的open方法返回的channel就已经帮我把代理设置好了.
    是否nio就没有提供socket proxy功能?
      

  5.   

    好像还有个
    java.net.ProxySelector
    看名字有nio的风格
      

  6.   

    在Mina的邮件组中发现了一段代码:
               SocketChannel ch = null;           Socket s = new Socket(socketProxy);
               s.setReuseAddress( true );
               ch = s.getChannel().open();呵呵,如此简单。汗!
      

  7.   

    SocketChannel ch = null;           Socket s = new Socket(socketProxy);
               s.setReuseAddress( true );
               ch = s.getChannel().open();
    以上代码不能运行。!
    继续汗!!!!!!
      

  8.   

    ch = s.getChannel()为null
    而null.open()居然不抱错!
      

  9.   

    InetAddress inetaddress=InetAddress.getByName("127.0.0.1") ;
    Socket socket = new Socket(inetaddress,1080);
    socket.setReuseAddress( true );
     ch   =socket.getChannel().open(); 怎么会没有结果呢?
    如果socket为空的话肯定会报错的。
    你跟踪一下。
      

  10.   

    我也想知道...请问LZ解决了没??正碰到这个...四处找都没有提及..我用的是xsocket..有人说NIO不支持设置代理...狂汗中..咋办..