Hi,

I'm trying to write a simple application that will utilize a Socks5
proxy server to connect to a web server (www.google.com).  I am using
an instance of 'java.net.Proxy' of type 'SOCKS' in my application and
am using my implementation of ProxySelector.  I can see from the
logcat output that my implementation of ProxySelector is being called
and will passback a list containing one Proxy instance.  When I run
this code in the emulator, I consistently get a JNI check error
resulting in the VM aborting.  I have attempted to run this
application with recent public releases of the SDK, all resulting in
the same behavior, JNI check failure and VM abort.

If anyone can shed some light on what I may be doing wrong in my code,
I would appreciate it.

Below find both the code and the logcat output:

=======
package com.test.socks;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Socks5ProxyTest extends Activity {

        private String TAG = Socks5ProxyTest.class.getSimpleName();

        private String socks5ProxyHost = "192.168.1.26";

        private int socks5ProxyPort = 1080;

        private TextView txtView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtView = (TextView) findViewById(R.id.text_view);

        ProxySelector.setDefault( new MyProxySelector());
        Authenticator.setDefault( new MyAuthenticator());

    }

        /* (non-Javadoc)
         * @see android.app.Activity#onDestroy()
         */
        @Override
        protected void onDestroy() {
                // TODO Auto-generated method stub
                super.onDestroy();
        }

        /* (non-Javadoc)
         * @see android.app.Activity#onPause()
         */
        @Override
        protected void onPause() {
                // TODO Auto-generated method stub
                super.onPause();
        }

        /* (non-Javadoc)
         * @see android.app.Activity#onRestart()
         */
        @Override
        protected void onRestart() {
                // TODO Auto-generated method stub
                super.onRestart();
        }

        /* (non-Javadoc)
         * @see android.app.Activity#onResume()
         */
        @Override
        protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
        }

        /* (non-Javadoc)
         * @see android.app.Activity#onStart()
         */
        protected void onStart() {
                super.onStart();

                String webURL = "http://www.google.com";;

                setStatus( "Setting up Http connection..." );
                URL url = null;
                HttpURLConnection httpUrlConn = null;
                try {
                        url = new URL( webURL );
                        URLConnection urlConn = url.openConnection();
                        if( urlConn instanceof HttpURLConnection ) {
                                httpUrlConn = (HttpURLConnection) urlConn;
                                httpUrlConn.setAllowUserInteraction(true);
                                httpUrlConn.setUseCaches(false);
                                setStatus( "Connecting to: " + webURL );
                                httpUrlConn.connect();
                                if( httpUrlConn.getResponseCode() == 
HttpURLConnection.HTTP_OK ) {
                                        setStatus( "Successfully connected to: 
" + webURL );
                                        InputStream inStr = 
httpUrlConn.getInputStream();
                                        if( inStr != null ) {
                                                BufferedReader bufRdr = null;
                                                try {
                                                        bufRdr = new 
BufferedReader( new InputStreamReader( inStr ));
                                                        String responseLine = 
null;
                                                        setStatus( "Reading 
lines from response..." );
                                                        while( (responseLine = 
bufRdr.readLine()) != null ) {
                                                                Log.i(TAG, 
responseLine );
                                                        }
                                                }
                                                finally {
                                                        if( bufRdr != null ) {
                                                                setStatus( 
"Closing body of response." );
                                                                bufRdr.close();
                                                        }
                                                }
                                        } else {
                                                Log.e(TAG, "null input stream!" 
);
                                        }
                                } else {
                                        Log.e(TAG, 
httpUrlConn.getResponseMessage());
                                }
                        }

                }
                catch( MalformedURLException urlExc ) {
                        Log.e(TAG, urlExc.getMessage(), urlExc );
                } catch (IOException ioExc) {
                        Log.e( TAG, ioExc.getMessage(), ioExc );
                }
                finally {

                        // Ensure we close the connection.
                        if( httpUrlConn != null ) {

                                setStatus( "Disconnecting from " + webURL );
                                httpUrlConn.disconnect();

                        }

                }

        }

        /* (non-Javadoc)
         * @see android.app.Activity#onStop()
         */
        @Override
        protected void onStop() {
                // TODO Auto-generated method stub
                super.onStop();
        }

        private void setStatus( final String status ) {

                runOnUiThread( new Runnable() {

                        public void run() {

                                txtView.setText(status);

                        }

                });

        }

        /**
         * Provides an extension of <code>ProxySelector</code> for specifying
a proxy for
         * a particular connection.
         *
         */
        class MyProxySelector extends ProxySelector {

                private List<Proxy> proxies;

                private final String TAG = 
MyProxySelector.class.getSimpleName();

                MyProxySelector() {

                        // Construct the list of proxies.
                        proxies = new ArrayList<Proxy>();
                        InetSocketAddress proxyAddress = new InetSocketAddress
( socks5ProxyHost, socks5ProxyPort );
                        proxies.add( new Proxy( Proxy.Type.SOCKS, proxyAddress 
));

                }

                /* (non-Javadoc)
                 * @see java.net.ProxySelector#connectFailed(java.net.URI,
java.net.SocketAddress, java.io.IOException)
                 */
                @Override
                public void connectFailed(URI uri, SocketAddress sa, IOException
ioe) {

                        Log.i(TAG, ": connectFailed: socket_address(" + 
sa.toString() + ")
IOException(" +
                                        ioe.getMessage() + ")" );

                }

                /* (non-Javadoc)
                 * @see java.net.ProxySelector#select(java.net.URI)
                 */
                @Override
                public List<Proxy> select(URI uri) {

                        Log.i(TAG, ": select on URI(" + uri.toString() + ")." );

                        // Always return the same list.
                        return proxies;

                }

        }

        /**
         * An extension of <code>Authenticator</code> for performing
authentication.
         */
        class MyAuthenticator extends Authenticator {

                private final String TAG = 
MyAuthenticator.class.getSimpleName();

                MyAuthenticator() {

                }

                /* (non-Javadoc)
                 * @see java.net.Authenticator#getPasswordAuthentication()
                 */
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {

                        Log.i(TAG, "getPasswordAuthentication: " );
                        // TODO Auto-generated method stub
                        return super.getPasswordAuthentication();
                }

                /* (non-Javadoc)
                 * @see java.net.Authenticator#getRequestingURL()
                 */
                @Override
                protected URL getRequestingURL() {

                        Log.i(TAG, "getRequestingURL: " );

                        // TODO Auto-generated method stub
                        return super.getRequestingURL();
                }

                /* (non-Javadoc)
                 * @see java.net.Authenticator#getRequestorType()
                 */
                @Override
                protected RequestorType getRequestorType() {

                        Log.i(TAG, "getRequestorType: " );

                        // TODO Auto-generated method stub
                        return super.getRequestorType();
                }

        }


==========
LogCat Output:

I/ActivityManager(   49): Starting activity: Intent
{ action=android.intent.action.MAIN categories=
{android.intent.category.LAUNCHER} flags=0x10200000 comp=
{com.test.socks/com.test.socks.Socks5ProxyTest} }
I/ActivityManager(   49): Start proc com.test.socks for activity
com.test.socks/.Socks5ProxyTest: pid=284 uid=10024 gids={3003}
I/jdwp    (  284): received file descriptor 20 from ADB
I/MyProxySelector(  284): : select on URI(http://www.google.com).
W/dalvikvm(  284): JNI WARNING: inst fieldID 0x410653e4 not valid for
class [B
I/dalvikvm(  284): "main" prio=5 tid=3 NATIVE
I/dalvikvm(  284):   | group="main" sCount=0 dsCount=0 s=0
obj=0x400103e8
I/dalvikvm(  284):   | sysTid=284 nice=0 sched=0/0 handle=-1096950628
I/dalvikvm(  284):   at
org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl
(Native Method)
I/dalvikvm(  284):   at
org.apache.harmony.luni.platform.OSNetworkSystem.connect
(OSNetworkSystem.java:119)
I/dalvikvm(  284):   at
org.apache.harmony.luni.net.PlainSocketImpl.socksConnect
(PlainSocketImpl.java:380)
I/dalvikvm(  284):   at
org.apache.harmony.luni.net.PlainSocketImpl.connect
(PlainSocketImpl.java:224)
I/dalvikvm(  284):   at
org.apache.harmony.luni.net.PlainSocketImpl.connect
(PlainSocketImpl.java:521)
I/dalvikvm(  284):   at java.net.Socket.connect(Socket.java:945)
I/dalvikvm(  284):   at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>
(HttpConnection.java:61)
I/dalvikvm(  284):   at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager
$ConnectionPool.getHttpConnection(HttpConnectionManager.java:145)
I/dalvikvm(  284):   at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection
(HttpConnectionManager.java:73)
I/dalvikvm(  284):   at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection
(HttpURLConnection.java:802)
I/dalvikvm(  284):   at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect
(HttpURLConnection.java:774)
I/dalvikvm(  284):   at com.test.socks.Socks5ProxyTest.onStart
(Socks5ProxyTest.java:103)
I/dalvikvm(  284):   at android.app.Instrumentation.callActivityOnStart
(Instrumentation.java:1204)
I/dalvikvm(  284):   at android.app.Activity.performStart
(Activity.java:3317)
I/dalvikvm(  284):   at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2113)
I/dalvikvm(  284):   at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2157)
I/dalvikvm(  284):   at android.app.ActivityThread.access$1800
(ActivityThread.java:112)
I/dalvikvm(  284):   at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1581)
I/dalvikvm(  284):   at android.os.Handler.dispatchMessage
(Handler.java:88)
I/dalvikvm(  284):   at android.os.Looper.loop(Looper.java:123)
I/dalvikvm(  284):   at android.app.ActivityThread.main
(ActivityThread.java:3739)
I/dalvikvm(  284):   at java.lang.reflect.Method.invokeNative(Native
Method)
I/dalvikvm(  284):   at java.lang.reflect.Method.invoke(Method.java:
515)
I/dalvikvm(  284):   at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:739)
I/dalvikvm(  284):   at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:497)
I/dalvikvm(  284):   at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(  284):
E/dalvikvm(  284): VM aborting
I/DEBUG   (   20): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***
I/DEBUG   (   20): Build fingerprint: 'generic/sdk/generic/:1.1/
PLATFORM-1_0/129975:sdk/test-keys'
I/DEBUG   (   20): pid: 284, tid: 284  >>> com.test.socks <<<
I/DEBUG   (   20): signal 11 (SIGSEGV), fault addr deadd00d
I/DEBUG   (   20):  r0 00000320  r1 0000000c  r2 0000000c  r3 00000026
I/DEBUG   (   20):  r4 deadd00d  r5 ad07e6f8  r6 4000d010  r7 43429a00
I/DEBUG   (   20):  r8 be9dd608  r9 41049aec  10 41049adc  fp 00000000
I/DEBUG   (   20):  ip ad07e7d4  sp be9dd548  lr afe1209d  pc
ad038a32  cpsr 20000030
I/DEBUG   (   20):          #00  pc ad038a32  /system/lib/libdvm.so
I/DEBUG   (   20):          #01  pc ad02bc00  /system/lib/libdvm.so
I/DEBUG   (   20):          #02  pc ad02bc56  /system/lib/libdvm.so
I/DEBUG   (   20):          #03  pc ad02dfa2  /system/lib/libdvm.so
I/DEBUG   (   20):          #04  pc ad21167e  /system/lib/
libnativehelper.so
I/DEBUG   (   20):          #05  pc ad214aba  /system/lib/
libnativehelper.so
I/DEBUG   (   20):          #06  pc ad00d2f4  /system/lib/libdvm.so
I/DEBUG   (   20): stack:
I/DEBUG   (   20):     be9dd508  00000005
I/DEBUG   (   20):     be9dd50c  00000000
I/DEBUG   (   20):     be9dd510  afe35c4c  /system/lib/libc.so
I/DEBUG   (   20):     be9dd514  afe35ca0  /system/lib/libc.so
I/DEBUG   (   20):     be9dd518  00000000
I/DEBUG   (   20):     be9dd51c  afe1209d  /system/lib/libc.so
I/DEBUG   (   20):     be9dd520  0000bb00  [heap]
I/DEBUG   (   20):     be9dd524  afe11249  /system/lib/libc.so
I/DEBUG   (   20):     be9dd528  43429a00
I/DEBUG   (   20):     be9dd52c  ad07e6f8  /system/lib/libdvm.so
I/DEBUG   (   20):     be9dd530  ad07e6f8  /system/lib/libdvm.so
I/DEBUG   (   20):     be9dd534  4000d010
I/DEBUG   (   20):     be9dd538  43429a00
I/DEBUG   (   20):     be9dd53c  afe112ad  /system/lib/libc.so
I/DEBUG   (   20):     be9dd540  df002777
I/DEBUG   (   20):     be9dd544  e3a070ad
I/DEBUG   (   20): #00 be9dd548  410653e4
I/DEBUG   (   20):     be9dd54c  ad02bc03  /system/lib/libdvm.so
I/DEBUG   (   20): #01 be9dd550  ad0660b0  /system/lib/libdvm.so
I/DEBUG   (   20):     be9dd554  ad02bc59  /system/lib/libdvm.so
I/ActivityManager(   49): Process com.test.socks (pid 284) has died.
D/Zygote  (   23): Process 284 terminated by signal (11)



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to