Vpnservice DatagramChannel.open()不起作用

我想创建应用程序,重新路由数据包。 我使用vpnservice(因为不需要root电话),使用toyVpn和这个代码http://www.thegeekstuff.com/2014/06/Android-vpn-service/但问题在这一行DatagramChannel tunnel = DatagramChannel.open() ; 这一行抛出异常。 这是我的代码

public class MyVpnService extends VpnService {

    public static String CONNECTION = "StartService";
    public static String DISCONNECTION = "StopService";
    public static boolean work = false;
    private Thread mThread;
    private ParcelFileDescriptor mInterface;
    Builder builder = new Builder();

    // Services interface
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Start a new session by creating a new thread.
        if (intent != null && DISCONNECTION.equals(intent.getAction())) {
            work = false;
            stopForeground(true);
            mThread.interrupt();
            stopSelf();

            return START_NOT_STICKY;
        }
        work = true;
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d("VPNservice", "0");
                    mInterface = builder.setSession("MyVPNService")
                            .addAddress("192.168.1.3", 24)
                            .addDnsServer("8.8.8.8")
                            .addRoute("0.0.0.0", 0).establish();
                    FileInputStream in = new FileInputStream(
                            mInterface.getFileDescriptor());
                    FileOutputStream out = new FileOutputStream(
                            mInterface.getFileDescriptor());
                    Log.d("VPNservice", "1 " + (mInterface != null));
                    DatagramChannel tunnel = DatagramChannel.open();
                    Log.d("VPNservice", "2 " + (tunnel != null));
                    Log.d("VPNservice", "3 " + tunnel.connect(new InetSocketAddress("127.0.0.1", 8087)));
                    //tunnel.connect(new InetSocketAddress("127.0.0.1", 8087));
                    tunnel.configureBlocking(false);
                    protect(tunnel.socket());
                    //e. Use a loop to pass packets.
                    ByteBuffer packet = ByteBuffer.allocate(Short.MAX_VALUE);
                    Log.d("VPNservice", "4");
                    while (!Thread.interrupted()) {
                        Log.d("VPNservice", "5");
                        int length = in.read(packet.array());
                        if (length > 0) {
                            packet.limit(length);
                            tunnel.write(packet);
                            // There might be more outgoing packets.
                            Log.d("VPNservice", "6");
                        }
                        Log.d("VPNservice", "7");
                        length = tunnel.read(packet);
                        Log.d("VPNservice", "8");
                        if (length > 0) {
                            if (packet.get(0) != 0) {
                                out.write(packet.array(), 0, length);
                                Log.d("VPNservice", "9");
                            }
                            packet.clear();
                            Log.d("VPNservice", "10");
                        }
                        Thread.sleep(100);
                        Log.d("VPNservice", "11");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (mInterface != null) {
                            mInterface.close();
                            mInterface = null;
                        }
                    } catch (Exception e) {

                    }
                }
            }

        }, "MyVpnRunnable");

        //start the service
        mThread.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        if (mThread != null) {
            mThread.interrupt();
        }
        super.onDestroy();
    }
}

这是日志:

 07-02 15:13:09.271 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-Calling onStartCommand: app.my.myvpnservice.MyVpnService@424f59f0, flags=0, startId=1
07-02 15:13:09.272 8853-8853/app.my.myvpnservice D/dalvikvm: create interp thread : stack size=128KB
07-02 15:13:09.272 8853-8853/app.my.myvpnservice D/dalvikvm: create new thread
07-02 15:13:09.274 8853-8853/app.my.myvpnservice D/dalvikvm: new thread created
07-02 15:13:09.274 8853-8853/app.my.myvpnservice D/dalvikvm: update thread list
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: interp stack at 0x7d83d000
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: created from interp
07-02 15:13:09.274 8853-8853/app.my.myvpnservice D/dalvikvm: start new thread
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: notify debugger
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11 (MyVpnRunnable): calling run()
07-02 15:13:09.275 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@424ef338 startId=1 args=Intent { act=StartService cmp=app.my.myvpnservice/.MyVpnService }}
07-02 15:13:09.275 8853-9304/app.my.myvpnservice D/VPNservice: 0
07-02 15:13:09.306 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-BIND_SERVICE handled : 0 / BindServiceData{token=android.os.BinderProxy@424ef338 intent=Intent { act=android.net.VpnService cmp=app.my.myvpnservice/.MyVpnService }}
07-02 15:13:09.447 8853-9304/app.my.myvpnservice D/VPNservice: 1 true
07-02 15:13:09.471 8853-9304/app.my.myvpnservice W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:622)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err:     at java.nio.DatagramChannelImpl.<init>(DatagramChannelImpl.java:75)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err:     at java.nio.SelectorProviderImpl.openDatagramChannel(SelectorProviderImpl.java:33)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err:     at java.nio.channels.DatagramChannel.open(DatagramChannel.java:66)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err:     at app.my.myvpnservice.MyVpnService$1.run(MyVpnService.java:50)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err:     at java.lang.Thread.run(Thread.java:848)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err:     at libcore.io.Posix.socket(Native Method)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err:     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:607)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: 	... 5 more
07-02 15:13:09.536 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: exiting
07-02 15:13:09.536 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: bye!
07-02 15:13:09.583 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-UNBIND_SERVICE handled : 0 / BindServiceData{token=android.os.BinderProxy@424ef338 intent=Intent { act=android.net.VpnService cmp=app.my.myvpnservice/.MyVpnService }}
链接地址: http://www.djcxy.com/p/31997.html

上一篇: Vpnservice DatagramChannel.open() not working

下一篇: urimatcher not working with prepopulated database