Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android利用LocalSocket實現Java端進程與C端進程之間的IPC

Android利用LocalSocket實現Java端進程與C端進程之間的IPC

編輯:Android開發教程

Android是建立在Linux之上的OS,在涉及到安全、網絡協議、文件加密等功能時,往往需要通過C語言調用底層API來實現,而如何發出指令讓C端執行我們想要的功能,並且在執行之後有返回結果呢,這就需要打通Java端進程和C端進程,使之能高效地通信。這樣,C端進程用於實現功能,Java端進程負責UI、功能的觸發及結果處理就可以了。

對於*nix系統來說,“一切皆為文件”,Socket也不例外,Socket按照收發雙方的媒介來說有三種類型:1,通過網絡端口;2,通過文件系統;3,通過內存映射文件。具體說來,三種類型均可以用來作為IPC的Socket:1,通過本地回環接口(即LoopBack)127.0.0.1來收發數據;2,通過文件作為收發數據的中轉站;3,在內存中開辟一塊區域作為收發數據的中轉站,此區域仍然使用文件讀寫API進行訪問。LocalSocket支持方式2和方式3,從效率的角度來說,顯然是方式3效率最高,那麼下面我們就使用LocalSocket來演示如何實現Java端進程與C端進程之間的IPC。

以下的demo是Java端作為server,C端作為client;實際場景中可能更多的是Java端作為client,而C端作為server。

服務端代碼如下:

package main.activity;
    
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
    
import android.app.Activity;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.os.Bundle;
import android.util.Log;
    
/**
 * @author pengyiming
 * @note 啟動localSocketServer
 *
 */
    
public class LocalSocketServerActivity extends Activity
{
    /* 數據段begin */
    private final String TAG = "server";
        
    private ServerSocketThread mServerSocketThread;
    /* 數據段end */
        
    /* 函數段begin */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
            
        mServerSocketThread = new ServerSocketThread();
        mServerSocketThread.start();
    }
        
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
            
        mServerSocketThread.stopRun();
    }
    /* 函數段end */
        
    /* 內部類begin */
    private class ServerSocketThread extends Thread
    {
        private boolean keepRunning = true;
        private LocalServerSocket serverSocket;
            
        private void stopRun()
        {
            keepRunning = false;
        }
            
        @Override
        public void run()
        {
            try
            {
                serverSocket = new LocalServerSocket("pym_local_socket");
            }
            catch (IOException e)
            {
                e.printStackTrace();
                    
                keepRunning = false;
            }
                
            while(keepRunning)
            {
                Log.d(TAG, "wait for new client coming !");
                    
                try
                {
                    LocalSocket interactClientSocket = serverSocket.accept();
                        
                    //由於accept()在阻塞時,可能Activity已經finish掉了,所以再次檢查keepRunning
                    if (keepRunning)
                    {
                        Log.d(TAG, "new client coming !");
                            
                        new InteractClientSocketThread(interactClientSocket).start();
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                        
                    keepRunning = false;
                }
            }
                
            if (serverSocket != null)
            {
                try
                {
                    serverSocket.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
        
    private class InteractClientSocketThread extends Thread
    {
        private LocalSocket interactClientSocket;
            
        public InteractClientSocketThread(LocalSocket interactClientSocket)
        {
            this.interactClientSocket = interactClientSocket;
        }
            
        @Override
        public void run()
        {
            StringBuilder recvStrBuilder = new StringBuilder();
            InputStream inputStream = null;
            try
            {
                inputStream = interactClientSocket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                char[] buf = new char[4096];
                int readBytes = -1;
                while ((readBytes = inputStreamReader.read(buf)) != -1)
                {
                    String tempStr = new String(buf, 0, readBytes);
                    recvStrBuilder.append(tempStr);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
                    
                Log.d(TAG, "resolve data error !");
            }
            finally
            {
                if (inputStream != null)
                {
                    try
                    {
                        inputStream.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    /* 內部類end */
}

查看本欄目更多精彩內容:http://www.bianceng.cn/OS/extra/

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved