Android Framework实战开发-socket跨进程通信之native程序与apk java通信

Posted Android高级知识分享官

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Framework实战开发-socket跨进程通信之native程序与apk java通信相关的知识,希望对你有一定的参考价值。

csdn在线学习课程,课程咨询答疑和新课信息:QQ交流群:422901085进行课程讨论

android跨进程通信实战视频课程(加群获取优惠)

unix socket前面几节课已经讲过了,但都是基于linux下面的c程序,那么在android中往往经常会遇到c代码的native程序要与
apk的java程序进行socket通信,本节课我们来实现一下:
直接上代码:
native端作为服务端:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/epoll.h>
#include <sys/un.h>
int main()
{
    char *socket_path = "server-socket";
    struct sockaddr_un serun, cliun;
    socklen_t cliun_len;
    int listenfd, connfd, size;
    char buf[80];
    int i, n;
    if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("socket error");
        exit(1);
    }
    //unlink(socket_path);
    memset(&serun, 0, sizeof(serun));
    serun.sun_family = AF_UNIX;
    serun.sun_path[0] = 0;
    strcpy(serun.sun_path+1,socket_path);
    socklen_t addrlen_ = sizeof(serun.sun_family) + strlen(socket_path) + 1;
    int ret = bind(listenfd, (struct sockaddr*)&serun,addrlen_);
    if(ret == -1)
    {
        perror("bind");
        exit(0);
    }
    // 3. 监听
    ret = listen(listenfd, 20);
    if(ret == -1)
    {
        perror("listen");
        exit(0);
    }

   
    while(1)
    {
	printf("wait connect...\\n");
	socklen_t l =   sizeof(struct sockaddr_un);
        int connfd = accept(listenfd, (struct sockaddr *)&cliun, &l);
        if(connfd == -1)
        {
            perror("accept");
            exit(-1);
        }
        printf("a new client connected! ");
    
        int count = read(connfd, buf, sizeof(buf));
        if(count == 0)//客户端关闭了连接
        {
            printf("客户端关闭了连接。。。。\\n");
          
        }
        else
        {
            if(count == -1)
            {
                perror("read");
                exit(-1);
            }
            else
            {
                //正常通信
                printf("client say: %s\\n" ,buf);
		write(connfd,"received ok",sizeof("received ok"));
            }
        }
    }
    close(listenfd);
    return 0;
}

apk端的java代码作为客户端:
只展示核心部分:

  private void sendMessage() {
        try {
            lsocket = new LocalSocket();
            LocalSocketAddress address = new LocalSocketAddress("server-socket", LocalSocketAddress.Namespace.ABSTRACT);
            lsocket.connect(address);
            String result;
            Log.i("test","rootclient send cmd to rootServer cmd = " + sendCmdText.getText().toString());
            br = new BufferedWriter(new OutputStreamWriter(lsocket.getOutputStream()));
            br.write(sendCmdText.getText().toString());
            br.newLine();
            br.flush();
//            br.close();

            Log.d("test", "========发送成功========");
            InputStream inputStream = lsocket.getInputStream();
            BufferedReader buffer = null;
            buffer = new BufferedReader(new InputStreamReader(inputStream));
            String tmpStr = null;
            while(inputStream.available() == 0) {
                try {
                //    Log.d("lsm", "========sleep======== inputStream.available() = " +inputStream.available());
                    Thread.sleep(1);
                }catch (Exception e) {

                }
            }
            byte[] buf = new byte[inputStream.available() -1];
            inputStream.read(buf,0,inputStream.available() -1) ;
            String str =new String(buf);
            Log.d("test", "========接受成功======== inputStream.available() = " +inputStream.available() + "read str  = " +str);
            final String str1 = str;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    receiveText.setText(str1);
                }
            });

            buffer.close();

            lsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("test","rootclient rootServer 发送失败",e);
        }
    }

ps:完整代码课程中会进行提供和讲解

以上是关于Android Framework实战开发-socket跨进程通信之native程序与apk java通信的主要内容,如果未能解决你的问题,请参考以下文章

千里马Android Framework实战开发-native程序之间binder通信实战案例分析

千里马Android Framework实战开发-native程序之间binder通信实战案例分析

千里马Android Framework实战开发-native程序之间binder通信实战案例分析

Android Framework实战开发-socketpair介绍及它在android系统源码使用分析

Android Framework实战开发-socketpair介绍及它在android系统源码使用分析

Android Framework实战开发-socketpair介绍及它在android系统源码使用分析