安卓 BSD 套接字连接



我在尝试将 BSD 客户端套接字连接到服务器时遇到了一些问题。套接字的创建和连接是通过 JNI 实现的。实际连接是通过 java 代码建立的。

JNI部分:

#include <jni.h>
#include <unistd.h>
#include <string.h>
#include <sys/endian.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <netinet/in.h>
JNIEXPORT jint JNICALL Java_com_example_socketclinet_Native_socket
(JNIEnv *, jclass, jint, jint, jint);
JNIEXPORT jint JNICALL Java_com_example_socketclinet_Native_connect
(JNIEnv *, jclass, jint, jint, jint);
jint JNICALL Java_com_example_socketclinet_Native_socket
(JNIEnv *env, jclass cls, jint domain, jint type, jint protocol)
{
    return socket(domain, type, protocol);
}
jint JNICALL Java_com_example_socketclinet_Native_connect
(JNIEnv *env, jclass cls, jint socket, jint address, jint port)
{
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(address);
    addr.sin_port = htons(port);
    return connect(socket, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in));
}

Java 本机桥类:

class Native
{
    static
    {
        System.loadLibrary("mylib");
    }
    public static final int SOCK_STREAM = 2;
    public static final int AF_INET = 2;
    public static native int socket(int domain, int type, int protocol);
    public static native int connect(int socket, int address, int port);
}

本机类用法:

int socket = Native.socket(Native.AF_INET, Native.SOCK_STREAM, 0);
if (socket < 0)
{
    System.err.println("Socket error: " + socket);
    return;
}
byte[] address = { .... }; // 192.168.xxx.xxx
int addr = address[0] << 24 | address[1] << 16 | address[2] << 8 | address[3];
int port = ....;
int result = Native.connect(socket, addr, port);
if (result < 0)
{
    System.err.println("Connection failed: " + result);
}
else
{
    System.out.println("Connected");
}

"connect"方法始终返回"0",即使没有服务器运行(在设备和模拟器上)。

• 我有清单文件设置的"INTERNET"权限(没有它,"套接字"函数返回 -1)
•相同的代码在iOS和Mac OS上运行良好。
• 测试环境:Nexus 5 (4.4.4),android-ndk-r10d

任何帮助将不胜感激!

byte[] 是用 Java 签名的,这意味着你的 |addr| 计算可能是错误的。我怀疑您正在连接到广播地址,根据定义,该地址将始终成功。

尝试从本机代码打印地址以验证,否则,请尝试将计算替换为:

int addr = (address[0] & 255) << 24 | 
           (address[1] & 255) << 16 |
           (address[2] & 255) <<  8 |
           (address[3] & 255);

看看这是否能解决问题。

最新更新