java/utilities/src/main/java/com/pravala/socket/SocketUtils.java
@Deprecated
    public static int bindSocketFd ( int socketFd, int netId )
    {
        try
        {
            Class< ? > networkUtilsClass
                = Class.forName ( "android.net.NetworkUtils", true, ClassLoader.getSystemClassLoader() );

            Method bindSocketToNetworkMethod = networkUtilsClass.getMethod (
                "bindSocketToNetwork", new Class[] { int.class, int.class } );

            // This directly calls bindSocketToNetwork. The first arg is the object with the method,
            // which is null in this case because it is a static method.
            int ret = ( Integer ) bindSocketToNetworkMethod.invoke ( null, socketFd, netId );

            // When bindSocketToNetwork fails (returns <0), the return code is a negative errno value.
            if ( ret < 0 )
                ret = -ret;

            return ret;
        }
        catch ( Exception e )
        {
            e.printStackTrace();

            return ENOSYS;
        }
    }
/**
     * Call Android's bindSocketToNetwork() function via reflection.
     *
     * On Android 7.0+ attempting to use a private native library from the wrong namespace will
     * cause a runtime error (i.e. using System.loadLibrary or dlopen). See:
     * https://developer.android.com/about/versions/nougat/android-7.0-changes.html#ndk
     *
     * This Java function can call bindSocketToNetwork() with no problems because it simply reflects out
     * the android.net.NetworkUtils.bindSocketToNetwork() method which internally calls bindSocketToNetwork()
     * and is allowed to do so.
     *
     * Then native code can call this static Java method through JNI whenever we need bindSocketToNetwork().
     *
     * @deprecated This function is deprecated. We now use official
     *             Network.bindSocket(FileDescriptor) API, introduced in API-23.
     *             We still need this version, to maintain compatibility with API-21 and API-22.
     *             Earlier versions do things using routing rules.
     *
     * @param socketFd The socket FD to bind to the network.
     * @param netId The Android netId that identifies a network.
     * @return 0 on success; otherwise a standard errno code indicating the failure reason.
     */
Call Android's bindSocketToNetwork() function via reflection.
On Android 7.0+ attempting to use a private native library from the wrong namespace will
cause a runtime error .

This Java function can call bindSocketToNetwork() with no problems because it simply reflects out
the android.net.NetworkUtils.bindSocketToNetwork() method which internally calls bindSocketToNetwork()
and is allowed to do so.

Then native code can call this static Java method through JNI whenever we need bindSocketToNetwork().

@deprecated This function is deprecated. We now use official
Network.bindSocket(FileDescriptor) API, introduced in API-23.
We still need this version, to maintain compatibility with API-21 and API-22.
Earlier versions do things using routing rules.

@param socketFd The socket FD to bind to the network.
@param netId The Android netId that identifies a network.
@return 0 on success; otherwise a standard errno code indicating the failure reason.
Call Android ' s bindSocketToNetwork () function via reflection . On Android 7 . 0 + attempting to use a private native library from the wrong namespace will cause a runtime error . This Java function can call bindSocketToNetwork () with no problems because it simply reflects out the android . net . NetworkUtils . bindSocketToNetwork () method which internally calls bindSocketToNetwork () and is allowed to do so . Then native code can call this static Java method through JNI whenever we need bindSocketToNetwork () . @deprecated This function is deprecated . We now use official Network . bindSocket ( FileDescriptor ) API introduced in API - 23 . We still need this version to maintain compatibility with API - 21 and API - 22 . Earlier versions do things using routing rules . @param socketFd The socket FD to bind to the network . @param netId The Android netId that identifies a network . @return 0 on success ; otherwise a standard errno code indicating the failure reason .
@Deprecated
    public static int bindSocketFd ( int socketFd, int netId )
    {
        try
        {
            Class< ? > networkUtilsClass
                = Class.forName ( "android.net.NetworkUtils", true, ClassLoader.getSystemClassLoader() );
            Method bindSocketToNetworkMethod = networkUtilsClass.getMethod (
                "bindSocketToNetwork", new Class[] { int.class, int.class } );
            int ret = ( Integer ) bindSocketToNetworkMethod.invoke ( null, socketFd, netId );
            if ( ret < 0 )
                ret = -ret;
            return ret;
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            return ENOSYS;
        }
    }
@ Deprecated public static int bindSocketFd ( int socketFd , int netId ) { try { Class < ? > networkUtilsClass = Class . forName ( "android.net.NetworkUtils" , true , ClassLoader . getSystemClassLoader ( ) ) ; Method bindSocketToNetworkMethod = networkUtilsClass . getMethod ( "bindSocketToNetwork" , new Class [ ] { int . class , int . class } ) ; int ret = ( Integer ) bindSocketToNetworkMethod . invoke ( null , socketFd , netId ) ; if ( ret < 0 ) ret = - ret ; return ret ; } catch ( Exception e ) { e . printStackTrace ( ) ; return ENOSYS ; } }
Call Android's bindSocketToNetwork() function via reflection.
Call Android ' s bindSocketToNetwork () function via reflection .
// This directly calls bindSocketToNetwork. The first arg is the object with the method, // which is null in this case because it is a static method. // When bindSocketToNetwork fails (returns <0), the return code is a negative errno value.