最近由于需要弄一些android底层的进程间通讯的东西,所以重温了一下linux的进程通信知识。

进程间通信有很多种方法,使用socket只是其中的一种方法,使用socket的好处呢,就是通用,代码可以在很多平台上直接编译,效率也很高的。

实际上,在android系统中,绝大多数的API 都使用了socket与底层的本地服务通信,这样做到了应用程序接口和底层框架分离的原则。用这种方式封装的API 更加安全,不存在代码进入内核态的问题,也不存在函数堆栈溢出的问题。所以说android系统比传统的操作系统更加安全是有道理的。

下面是一个通用的linux的socket IPC的例子,这个例子的详细解释可以参照这里的描述:http://blog.csdn.net/xnwyd/article/details/7359506

这个例子的好处呢,就是通用,在所有的linux系统下均可以编译使用。经测试,在x86和arm下都可以正常使用。

这个例子我编译的时候使用了arm c语言的交叉编译环境,至于如何搭建arm交叉编译环境,可以参照这里的方法:http://tweetyf.org/2013/04/using_the_android_toolchain_as_a_standalone_compiler.html 另外,如果你有android的源代码编译环境,且需要把自己编译的本地服务集成到系统中的话,有更加简洁的方法创建和编译系统服务,这个下回再做介绍。

server.c 编译方法:arm-linux-androideabi-gcc -o server server.c

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
#include<stdlib.h>
#include <stddef.h>

#define SERVER_NAME "@server_socket"
/*
 * Create a UNIX-domain socket address in the Linux "abstract namespace".
 *
 * The socket code doesn't require null termination on the filename, but
 * we do it anyway so string functions work.
 */
int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
{
    int nameLen = strlen(name);
    if (nameLen >= (int) sizeof(pAddr->sun_path) -1)  /* too long? */
        return -1;
    pAddr->sun_path[0] = '\0';  /* abstract namespace */
    strcpy(pAddr->sun_path+1, name);
    pAddr->sun_family = AF_UNIX;
    *pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
    return 0;
}

int main()

{
    int server_sockfd, client_sockfd;
    socklen_t server_len, client_len;
    struct sockaddr_un server_addr;
    struct sockaddr_un client_addr;
    char ch;
    int nread;

    //delete the old server socket
    //unlink("server_socket");
    //create socket
    server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

    //name the socket
    server_addr.sun_family = AF_UNIX;
    strcpy(server_addr.sun_path, SERVER_NAME);
    server_addr.sun_path[0]=0;
    //server_len = sizeof(server_addr);
    server_len = strlen(SERVER_NAME)  + offsetof(struct sockaddr_un, sun_path);
    //makeAddr("server_socket", &server_addr, &server_len);
    bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);

    //listen the server
    listen(server_sockfd, 5);
    client_sockfd = -1;
    client_len = sizeof(client_addr);
    while(1){
                printf("server waiting...\n");
                //accept client connect
                if(client_sockfd == -1){
                        client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_addr, &client_len);
                }

                //read  data from client socket
                nread = read(client_sockfd, &ch, 1);
                if(nread == 0){//client disconnected
                        printf("client %d disconnected\n",client_sockfd);
                        client_sockfd = -1;
                }
                else{
                        printf("read from client %d: %c\n",client_sockfd,ch);
                        ch ++;
                        write(client_sockfd, &ch, 1);
                }
                usleep(100);//1000 miliseconds = 1 second
    }

    return 0;

}

client.c 编译:arm-linux-androideabi-gcc -o client client.c

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
#include<stdlib.h>
#include <stddef.h>
#define SERVER_NAME "@server_socket"
/*
 * Create a UNIX-domain socket address in the Linux "abstract namespace".
 *
 * The socket code doesn't require null termination on the filename, but
 * we do it anyway so string functions work.
 */
int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
{
    int nameLen = strlen(name);
    if (nameLen >= (int) sizeof(pAddr->sun_path) -1)  /* too long? */
        return -1;
    pAddr->sun_path[0] = '\0';  /* abstract namespace */
    strcpy(pAddr->sun_path+1, name);
    pAddr->sun_family = AF_UNIX;
    *pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
    return 0;
}

int main()
{
        int sockfd;
        socklen_t len;
        struct sockaddr_un address;
        int result;
        char ch = 'A';

        //create socket
        sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
        //name the server socket
        //makeAddr("server_socket", &address, &len);
        address.sun_family = AF_UNIX;
        strcpy(address.sun_path, SERVER_NAME);
        address.sun_path[0]=0;
        //len = sizeof(address);
        len =  strlen(SERVER_NAME)  + offsetof(struct sockaddr_un, sun_path);
        //connect to server
        result = connect(sockfd, (struct sockaddr*)&address, len);
        if(result == -1)
        {
                perror("opps:client1");
                exit(1);
        }
        //communicate with server socket
        while(1)
        {
                printf("set send content:");
                scanf("%c",&ch);
                write(sockfd, &ch, 1);
                printf("send to server:%c \n",ch);
                read(sockfd, &ch, 1);
                printf("read from server: %c\n", ch);

        }
        exit(0);

}

分别把client 和 server 上传到arm的板子或者 android手机上,用命令行执行就可以测试效果了。