簡體   English   中英

在Android Native應用程序中無法創建客戶端套接字。 錯誤代碼:14無法創建套接字:權限被拒絕

[英]In Android Native app failed to create a client socket. Error code:14 Unable to create socket: Permission denied

我創建了一個使用本機代碼調用方法的Android應用。 我無法創建套接字錯誤。 以下代碼編譯為libconnect.so。 我已經使用(System.loadLibrary(“ connect”)在Android應用程序中加載了此庫。

#include<gio/gio.h>
#include<glib.h>
#include<android/log.h>
#include "connect.h"

int connect()
{
    GSocketConnection *connection=NULL;
    GSocketClient *client;
    GSocketAddress *address;
    GCancellable *cancellable=NULL;
    GError *error=NULL;
    address = g_network_address_new("192.168.0.1",8080);
    if(address == NULL)
        __android_log_print(6,"Connect Method","Address is not valid");
    client = g_socket_client_new();

    connection = g_socket_client_connect(client, (GSocketConnectable *)address, cancellable, &error);
    __android_log_print(6,"Connect Method","Connecting... ");
    if(connection == NULL)
        __android_log_print(6,"Connect Method","Connection is null");
    if(error != NULL)
        __android_log_print(6,"Connect Method","Error code: %d , Error msg: %s",error->code,error->message);
    return 0;
}

我收到以下日志:

12-31 11:38:18.032: E/Connect Method(2330): Connecting... 
12-31 11:38:18.032: E/Connect Method(2330): Connection is null
12-31 11:38:18.032: E/Connect Method(2330): Error code: 14 , Error msg: Unable to create socket: Permission denied

我有一個服務器在端口8080的192.168.0.1中運行。我需要該應用程序才能連接到該服務器並建立TCP連接。 錯誤代碼14指定什么? 如何解決這個錯誤?

應該在AndroidManifest.xml中添加了“ android.permission.INTERNET”和“ android.permission.ACCESS_NETWORK_STATE” android權限。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM