与Unix领域中的Andr​​oid NDK LocalSocket通信领域、通信、Andr、Unix

由网友(_____旧情劫′)分享简介:我的Andr​​oid应用程序,需要建立与Unix域套接字连接我们的C ++库(使用Android NDK)I have Android application, which needs to establish unix domain socket connection with our C++ library...

我的Andr​​oid应用程序,需要建立与Unix域套接字连接我们的C ++库(使用Android NDK)

I have Android application, which needs to establish unix domain socket connection with our C++ library (using Android NDK)

public static String SOCKET_ADDRESS = "your.local.socket.address"; // STRING

有是在java中LocalSocket它接受串(your.local.socket.address)

There is LocalSocket in java which accepts "string" (your.local.socket.address)

#define ADDRESS     "/tmp/unix.str" /* ABSOLUTE PATH */
  struct sockaddr_un saun, fsaun;
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("server: socket");
        exit(1);
    }
    saun.sun_family = AF_UNIX;
    strcpy(saun.sun_path, ADDRESS);

但它是在原生层Unix域套接字接受绝对路径。那么,怎样才能这两方传达给对方?

But the unix domain socket which is at native layer accepts "absolute path". So how can these two parties communicate to each other?

请分享任何例如,如果可能的

Please share any example if possible

推荐答案

LocalSocket使用Linux抽象名字空间,而不是文件系统。在C语言中,这些地址由prepending' 0'到指定的路径。

LocalSocket uses the Linux abstract namespace instead of the filesystem. In C these addresses are specified by prepending '' to the path.

const char name[] = "your.local.socket.address";
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;

// size-1 because abstract socket names are *not* null terminated
memcpy(addr.sun_path, name, sizeof(name) - 1);

另外请注意,你不应该通过的sizeof(sockaddr_un)绑定 SENDTO ,因为继' 0'字符的所有字节PTED作为抽象的套接字名称间$ p $。计算并通过实际大小,而不是:

Also note that you should not pass sizeof(sockaddr_un) to bind or sendto because all bytes following the '' character are interpreted as the abstract socket name. Calculate and pass the real size instead:

int res = sendto(sock, &data, sizeof(data), 0,
                 (struct sockaddr const *) &addr,
                 sizeof(addr.sun_family) + sizeof(name) - 1);
阅读全文

相关推荐

最新文章