开发者

Android, Bad Socket Exception (SOCKS)

开发者 https://www.devze.com 2023-01-20 12:26 出处:网络
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:

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 开发者_如何学JAVAgave 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消