Android uses sockets to complete interprocess communication

Source: Internet
Author: User



Look at some Android applications, especially related to the underlying functional applications, such as game acceleration, modify memory, hang machine script God horse, found inside the communication mechanism without exception is the use of sockets, basically has become a standard for such applications.

Because this kind of application has the following several common points:


1 need Android phone root permission, after all, to modify some of the lower level of things, no root permissions sometimes wood can modify AH


2 have their own so, at the same time more important or more strenuous work compiled into an executable elf file, and then let the APK app to this elf file to start up. There are several options for where this elf file is placed.


Scenario 1: Place directly under the Lib directory under the app package and give the executable permission to execute it.


Scenario 2: Under the Assets folder, copy to the Files folder when the program starts, give the executable permission, and then execute


Case 3: Also put under the Assets folder, but the security is a little higher, generally do a simple XOR or shift encryption, and then modify the following extension, copy the past time to reduce the density. execution, and sometimes the actual elf file will be deleted after execution.


3 The executable's communication between the so file and the Java application uses the socket, because after all the executable so is the real work of the main, and the Java application to send instructions to so, and so also to the Java application feedback some useful data for the upper layer display to the user. There are several scenarios for how the socket communicates


Scenario 1: Executable so as server side, the Java application to send instructions when the initiative to link executable so, send data, and sometimes so will also return some data, generally this situation so directly to the work, such as the game and deceleration.


Scenario 2: Executable so file as a client, while using long links, this situation requires the Java application as server side, here is slightly more clever is not to adopt a fixed socket port number, Instead, the Java application uses the system's automatically assigned port number when it creates the socket. However, a Java application is required to pass the port number to the so file when the executable is started.


The principle is basically so much, the following is an example, this example relates to the programming technology is the Java application itself to create the socket client and server side, and then to communicate, but also launched a so executable, through the process of data flow, to the executable to send data, The executable will print the received data through log.


Into the demo example,


Click Start Socket Server


This time, log will print.


11-04 15:56:28.341:v/cheatecore-server (5774): Server port = 44034


Such data,


Because serversocket = new ServerSocket (m_port);


Where M_port is 0, the port number is temporarily assigned by the system.


Then Socketserver began to wait for a client socket to link


M_socket = Serversocket.accept ();


Where accept is a blocking function that will block this thread out. This time


Access Android phone via adb shell, using Netstat–an | BusyBox grep 44034 View, found to be such a phenomenon


Obviously this port is listen state


Then click Set client socket port number and start client socket


11-04 16:03:11.067:v/cheatecore (5774): Gets the port number on the server side: 44034
11-04 16:03:15.575:v/cheatecore-client (5774): Client socket BEGIN
11-04 16:03:15.575:v/cheatecore-server (5774): A client socket come here!!!


This time, a client socket comehere was present!!!
This log is executed again just now
Netstat–an | BusyBox grep 44034
The discovery has become the result.



A randomly assigned client port number has been linked to the server side.


Then click Get Data stream


reader = new BufferedReader (Inputreader);
Serveroutwriter = new PrintWriter (Newbufferedwriter (New OutputStreamWriter (Sk.getoutputstream ())), true);


The main acquisition socket of the input stream and output stream, that is, the client and the server side of the TCP link has been established, the rest is through the stream to receive and send the data


Then click
Start the server socket receive data thread


String Sreader = Reader.readline ();
This statement is waiting for the client socket data come over, where readline is a blocking function, familiar with C language classmate, in fact, this is the socket in the RECV function, blocking calls.




Because TCP is full-duplex and the current state is a long link, the server can send data to the client, and the client can send data to the server, the Next button


Server-side socket sends data


11-04 16:11:19.810:v/cheatecore-client (5774): Client receives data from server:
11-04 16:11:19.810:v/cheatecore-server (5774): Client-side data received by the server: Clientdata


This time the log, the client after receiving the server's data, will also return to the server back data, so that the completion of an interaction, but in fact, this time the socket is not broken, because it is a long link, can be sent multiple times


Finally end socket


Close the socket and use Netstat–an at this time | BusyBox grep 44034
This command is to be queried and found such as:



The socket you just created has been released, and after a while the wait timeout is queried, the port is also released


Test sends data to the process started


11-04 16:15:11.278:e/cheatecore ( 6163): input target main function!!!!
11-04 16:15:11.278:e/cheatecore (6163): scanf input content is 44034


The main process is to start first


/data/data/ Com.example.sockettest/files/target
This program


Testprocee =runtime.getruntime (). exec (cmd);
Printwriterpoutwriter = Newprintwriter (Newbufferedwriter (Newoutputstreamwriter (Testprocee.getoutputstream ())), true);  
poutwriter.println (test);
The

then sends the data to complete the interaction between the processes, which is also a way of communicating between processes and using the socket to complete the communication between processes.

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: info-contact@alibabacloud.com 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.