Regarding socket communication between Android applications and Framework, I believe that my friends who are concerned about this issue have read this article "using socket for android to enable communication between the underlying layer and framework, the fly in the ointment is that the author only posts some key code snippets without releasing the source code. Based on an actual running example, it is also easy for you to learn.
First, let's take a look at the effect, as shown in. I fill in the name "Potter", select the Gender "Mr", and then click send. After receiving the message, the underlying socket directly returns the message to me. I will return the result (Mr. potter) is directly displayed in Result.
Write the socket server code to generate the executable script ht22.
# Define SOCKET_NAME "htpsk" # include <stdio. h> # include <stdlib. h> # include <errno. h> # include <string. h> # include <sys/types. h> # include <netinet/in. h> # include <sys/socket. h> # include <sys/wait. h> # include <sys/un. h> # include <cutils/sockets. h> # include <utils/Log. h> # include <android/log. h> int main () {char log [200]; int connect_number = 6; int fdListen =-1, new_fd =-1; int ret; struct sockaddr_un peeraddr; socklen_t socklen = sizeof (peeraddr); int numbytes; char buff [256]; // this step is critical to get init. socket fdListen = android_get_control_socket (SOCKET_NAME); if (fdListen <0) {sprintf (log, "Failed to get socket '" SOCKET_NAME "'errno: % d ", errno) ;__ android_log_write (ANDROID_LOG_DEBUG," FTM_JNI ", log); exit (-1) ;}// start to listen to ret = listen (fdListen, connect_number ); sprintf (log, "Listen result % d", ret); _ android_log_write (ANDROID_LOG_DEBUG, "FTM_JNI", log); if (ret <0) {perror ("listen"); exit (-1) ;}// wait for the Socket Client to send the connection request new_fd = accept (fdListen, (struct sockaddr *) & peeraddr, & socklen); sprintf (log, "Accept_fd % d", new_fd); _ android_log_write (ANDROID_LOG_DEBUG, "FTM_JNI", log); if (new_fd <0) {sprintf (log, "% d", errno); _ android_log_write (ANDROID_LOG_DEBUG, "FTM_JNI", log); perror ("accept error "); exit (-1);} while (1) {// wait until the Socket Client sends a message _ android_log_write (ANDROID_LOG_DEBUG, "FTM_JNI", "Waiting for receive "); if (numbytes = recv (new_fd, buff, sizeof (buff), 0) =-1) {sprintf (log, "% d", errno ); _ android_log_write (ANDROID_LOG_DEBUG, "FTM_JNI", log); perror ("recv"); continue;} // send a message receipt to the Socket client if (send (new_fd, buff, strlen (buff), 0) =-1) {perror ("send"); close (new_fd); exit (0) ;}} close (new_fd ); close (fdListen); return 0 ;}
3. Compile the client java code. The core code is as follows:
Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import android.net. localSocket; import android.net. localSocketAddress; import android. util. log;/*** Socket client ** @ author lai_zs * @ date: 12:15:09 */public class SocketClient {private final String SOCKET_NAME = "htpsk"; private LocalSocket client; private LocalSocketAddress Ddress; private boolean isConnected = false; private int connetTime = 1; public SocketClient () {client = new LocalSocket (); address = new LocalSocketAddress (SOCKET_NAME, LocalSocketAddress. namespace. RESERVED); new ConnectSocketThread (). start ();}/*** send message ** @ param msg * @ return returns the message receipt of the Socket server */public String sendMsg (String msg) {if (! IsConnected) {return "Connect fail";} try {BufferedReader in = new BufferedReader (new InputStreamReader (client. getInputStream (); PrintWriter out = new PrintWriter (client. getOutputStream (); out. println (msg); out. flush (); return in. readLine ();} catch (IOException e) {e. printStackTrace ();} return "Nothing return";}/*** asynchronously connects to the Socket. If the Socket fails to be connected, repeat the connection for 10 times. ** @ author Administrator **/private class ConnectSocketT ETT Hread extends Thread {@ Overridepublic void run () {while (! IsConnected & connetTime <= 10) {try {sleep (1000); Log. I ("SocketClient", "Try to connect socket; ConnectTime:" + connetTime); client. connect (address); isConnected = true;} catch (Exception e) {connetTime ++; isConnected = false; Log. I ("SocketClient", "Connect fail") ;}}}/*** close Socket */public void closeSocket () {try {client. close ();} catch (IOException e) {e. printStackTrace ();}}}