在Android Native应用程序中无法创建客户端套接字。错误代码:14无法创建套接字:权限被拒绝

时间:2014-12-31 06:21:40

标签: java android sockets java-native-interface native

我创建了一个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指定了什么?如何解决此错误?

1 个答案:

答案 0 :(得分:2)

应该在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>