日期:2014-05-20  浏览次数:20675 次

如何设置socket代理?
采用老的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代理?

------解决方案--------------------
路过 mark一下
------解决方案--------------------
帮顶
------解决方案--------------------
学习
------解决方案--------------------
方式不同,思想应该一样吧
------解决方案--------------------
UP

------解决方案--------------------
高手快进来呀
------解决方案--------------------