socket failure when using NDK r5b

This topic contains 4 replies, has 2 voices, and was last updated by  Bhanu Chetlapalli 6 years, 4 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #31361

    wzlx2000
    Member

    We have a problem, who can help me?

    We are using MIPS's NDK r5b to develop the android application. (We download the MIPS's NDK from this website:  https://www.imgtec.com/mips/mips/developers/download-android-ndk.asp)

    We currently got a problem. Here has a  listen error when we use the socket API based on C language to establish the network communication. The listen API function

    return -1. We have tried to modify the parameters of socket API, but  it alse has this error. The source code are as follows. Please help me!

     

    #include <android/log.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

     

    jstring
    Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                      jobject thiz )
    {
     int sockfd;
     struct sockaddr_in my_addr;
     struct sockaddr_in their_addr;
     int addr_len, numbytes;

     char buf[4096];
     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
     {
      __android_log_print(ANDROID_LOG_INFO, “InitializeCommon”,”Server-socket() sockfd error lol! errno = %d”,errno);
         return (*env)->NewStringUTF(env, “Server-socket() sockfd error lo”);

     }
     int port = 9129;
     my_addr.sin_family = AF_INET;
     my_addr.sin_port = htons(port);
     my_addr.sin_addr.s_addr = INADDR_ANY;
     memset(&(my_addr.sin_zero), '', 8);
     if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
     {
      __android_log_print(ANDROID_LOG_INFO, “InitializeCommon”,”Server-bind() sockfd error lol! port = %d errno = %d”,port,errno);
      return (*env)->NewStringUTF(env, “Server-bind() sockfd error lo”);
     }
     else
      __android_log_print(ANDROID_LOG_INFO, “InitializeCommon”,”Server-bind() is OK… port = %d”,port);
     if(listen( sockfd, 3) == -1 )
     {
      __android_log_print(ANDROID_LOG_INFO, “InitializeCommon”,”Error listening %dn”,errno);
      return (*env)->NewStringUTF(env, “Error listening”);
     }

     close(sockfd);
         return (*env)->NewStringUTF(env, “Hello from JNI !”);
    }

     

    #37755

    Does your program explicitly request for INTERNET permission? If not, the problem you are facing is a permission issue. When you create an application which opens Sockets and does socket communication – while in Java or Native Code, you need to request the android.permission.INTERNET permission. That way the user knows that an application which does not request this, cannot send network traffic without user knowledge.

     

    To do this, add this line to you AndroidManifest.xml , Check google SDK docs for more information.

     

    <uses-permission android:name=”android.permission.INTERNET” />

     

    #37756

    wzlx2000
    Member

    My AndroidManifest.xml has the following items. That is to say my application had requested the android.permission.INTERNET permission. It is not the key point. I think that the NDK r5b for MIPS may has some problem, then led to this problem. Who has extensive experience about NDK r5b for MIPS?Please help me.

     

    <uses-permission android:name=”android.permission.INTERNET” />
    <uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
    <uses-permission android:name=”android.permission.WAKE_LOCK” />
    <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
    <uses-permission android:name=”android.permission.ADD_SYSTEM_SERVICE” />

    #37757

    wzlx2000
    Member

    Please look at my AndroidManifest.xml files. It has added the INTERNET permission, But it also has the same error.

    

     

    <?xml version=”1.0″ encoding=”utf-8″?>
    <manifest xmlns:android=”http://schemas.android.com/apk…..es/android“
          package=”com.example.hellojni”
          android:versionCode=”1″
          android:versionName=”1.0″>
        <uses-sdk android:minSdkVersion=”3″ />   
        <uses-permission android:name=”android.permission.INTERNET” />
      <uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
      <uses-permission android:name=”android.permission.WAKE_LOCK” />
      <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
      <uses-permission android:name=”android.permission.ADD_SYSTEM_SERVICE” />
      <uses-permission android:name=”android.permission.ACCESS_CHECKIN_PROPERTIES” />
      <uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
      <uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
      <uses-permission android:name=”android.permission.ACCESS_LOCATION_EXTRA_COMMANDS” />
      <uses-permission android:name=”android.permission.ACCESS_MOCK_LOCATION” />
      <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
      <uses-permission android:name=”android.permission.ACCESS_SURFACE_FLINGER” />
      <uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
      <uses-permission android:name=”android.permission.ADD_SYSTEM_SERVICE” />
      <uses-permission android:name=”android.permission.BATTERY_STATS” />
        <application android:label=”@string/app_name”
                     android:debuggable=”true”>
            <activity android:name=”.HelloJni”
                      android:label=”@string/app_name”>
                <intent-filter>
                    <action android:name=”android.intent.action.MAIN” />
                    <category android:name=”android.intent.category.LAUNCHER” />
                </intent-filter>
            </activity>
        </application>
    </manifest>

    #37758

    Please apply this patch to the header files in the NDK, to fix this issue.

    
    --- s1/platforms/android-8/arch-mips/usr/include/sys/socket.h
    +++ s2/platforms/android-8/arch-mips/usr/include/sys/socket.h
    @@ -34,12 +34,22 @@

    __BEGIN_DECLS

    +#ifdef __mips__
    +#define SOCK_DGRAM 1
    +#define SOCK_STREAM 2
    +#define SOCK_RAW 3
    +#define SOCK_RDM 4
    +#define SOCK_SEQPACKET 5
    +#define SOCK_DCCP 6
    +#define SOCK_PACKET 10
    +#else
    #define SOCK_STREAM 1
    #define SOCK_DGRAM 2
    #define SOCK_RAW 3
    #define SOCK_RDM 4
    #define SOCK_SEQPACKET 5
    #define SOCK_PACKET 10
    +#endif

    #ifdef __i386__
    # define __socketcall extern __attribute__((__cdecl__))

     

Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic.