Android leverages Localsocket to implement IPC between Java-side processes and C-end processes

Source: Internet
Author: User
Tags file system socket thread

Android is built on Linux on the OS, when it comes to security, network protocols, file encryption and other functions, often need to call the underlying API in C language to implement, and how to send instructions to the C side to perform the functions we want, and after the implementation of the return results? This requires the Java-side process and the C-side process to be made to communicate efficiently. Thus, the C-end process is used to implement functionality, and the Java-side process is responsible for UI, function triggering, and result processing.

For *nix system, "Everything is file", socket is no exception, socket according to send and receive both sides of the media has three types: 1, through the network port; 2, through the file system, 3, through the memory-mapped file. In particular, three types can be used as IPC socket:1, through the local loop interface (ie loopback) 127.0 0.1来 send and receive data, 2, through the file as a transit point to send and receive data, 3, in memory to open up a region as a transit point to send and receive data, This area is still accessed using the file read-write API. Localsocket support mode 2 and Mode 3, from an efficiency point of view, is obviously the most efficient way 3, then we use Localsocket to demonstrate how to implement IPC between Java-side processes and C-end processes.

The demo below is the Java end as the Server,c client; the actual scenario may be more of a Java end as a client, and a C end as a server.

The service-side code is as follows:

Package main.activity;
Import Android.os.Bundle;
Import Android.util.Log; 
/** * @author pengyiming * @note Start localsocketserver * * */public class Localsocketserveractivity extends activity
    {/* Data section BEGIN/* Private final String TAG = "Server";
    Private Serversocketthread Mserversocketthread;
        /* Data segment End/* Function section begin/* protected void OnCreate (Bundle savedinstancestate) {
        Super.oncreate (savedinstancestate);
        Mserversocketthread = new Serversocketthread ();
    Mserversocketthread.start ();
        } @Override protected void OnDestroy () {Super.ondestroy ();
    Mserversocketthread.stoprun (); }/* Function segment End/////PrivaTe class Serversocketthread extends Thread {private Boolean keeprunning = true;
        Private Localserversocket ServerSocket;
        private void Stoprun () {keeprunning = false; @Override public void Run () {try {ServerS
            Ocket = 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 ();
                    Since accept () may have been done with the activity when it is blocked, check keeprunning if (keeprunning) again
 {                       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 = inte RactclientsoCket; @Override public void Run () {StringBuilder Recvstrbuilder = new String
            Builder ();
            InputStream inputstream = null;
                try {InputStream = Interactclientsocket.getinputstream ();
                InputStreamReader InputStreamReader = new InputStreamReader (InputStream);
                char[] buf = new char[4096];
                int readbytes =-1; while ((Readbytes = (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 (); }}}}/* Inner class End */}

See more highlights of this column: http://www.bianceng.cn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.