views:

16

answers:

1

I tried to connect to a server through a SOCKS server in an Android client app (Android OS 1.5 and 1.6) with below snippet code:

    Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(Constants.SOCKS_PROXY_HOST, Constants.SOCKS_PROXY_PORT));
    socket = new Socket(proxy);
    InetSocketAddress dest = new InetSocketAddress(targetHost,targetPort);
    socket.connect(dest);

However it gave out an exception on the last line:

W/System.err(  507): java.net.SocketException: SOCKS connection failed: java.net.SocketException: Bad socket
W/System.err(  507):    at org.apache.harmony.luni.net.PlainSocketImpl.socksConnect(PlainSocketImpl.java:389)
W/System.err(  507):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:224)
W/System.err(  507):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:521)
W/System.err(  507):    at java.net.Socket.connect(Socket.java:1019)
W/System.err(  507):    at java.net.Socket.connect(Socket.java:960)

Anyone might have an idea where did i do wrong? Thanks in advance.

A: 

It took a while but finally found the solution. It seems that there's a bit problem with java.net SOCKS implementation on several Android devices, even when both phones have the same Android OS version (tested on several phones with different builds). Anyway, I have to use another alternative socks library on my project: JSocks (http://jsocks.sourceforge.net/)

And so far it works like a charm so far on several devices i've tested.

anmustangs