Http://www.linuxidc.com/Linux/2011-11/47588.htm
Data communication between the Java layer and native using socket in Android
Android is a Linux-based system, and the underlying mechanism of the system is basically the same, because it is divided into local code and Java code, and Java code is called to execute local code through JNI.
Because UDP packet transmission is limited, the limitation is that when the sender's speed far exceeds the receiver's, a large amount of data will be lost at the underlying layer, in addition, UDP can easily cause port blocking. Therefore, more powerful classes are required to implement similar functions. Localserversocket and localsocket are generated under such circumstances. They store data in the buffer zone using UNIX space.
Example 1: native local code is used as the client to send requests, Java is used as the server to create a socket, and listening to and processing socket requests:
Import android.net. localserversocket;
Import android.net. localsocket;
Class socketlistener extends thread {
@ Override
Public void run (){
Try {
Localserversocket Server = new localserversocket ("com. Jenny. VNC. localsocket ");
While (true ){
Localsocket receiver ER = server. Accept ();
If (else er! = NULL ){
Inputstream input = ER er. getinputstream ();
Int readed = input. Read ();
...... // Handle
}
}
Server. Close ();
} Catch (ioexception e ){
Log. E (getclass (). getname (), E. getmessage ());
}
}
}
The native code of JNI is used as the client:
# Include <sys/socket. h>
Void send_remote_request (char * MSG)
{
Int localsocket, Len;
Struct sockaddr_un remote;
If (localsocket = socket (af_unix, sock_stream, 0) =-1 ){
Exit (1 );
}
Char * name = "com. Jenny. VNC. localsocket"; // It is the same as the Java upper layer.
Remote. sun_path [0] = '\ 0';/* abstract namespace */
Strcpy (remote. sun_path + 1, name );
Remote. sun_family = af_unix;
Int namelen = strlen (name );
Len = 1 + namelen + offsetof (struct sockaddr_un, sun_path );
If (connect (localsocket, (struct sockaddr *) & remote, Len) =-1 ){
Return;
}
If (send (localsocket, MSG, strlen (MSG), 0) =-1 ){
Return;
}
Close (localsocket );
}
Example 2: Java is used as the client to send requests, and native local code is used as the server to listen to and process socket requests:
Main Code of the Java layer:
1. localsocket so = NULL;
2. localsocketaddress ADDR;
3. So = new localsocket ();
4. ADDR = new localsocketaddress (socket_name, localsocketaddress. namespace. Reserved );
5. So. Connect (ADDR );
If you can connect to ADDR normally, you can perform I/O read and write operations like normal file operations.
Main Code of the native layer:
1. cli_fd = android_get_control_socket (socket_name );
2. retval = listen (cli_fd, backlog );
3. cli_fd_cmd = accept (cli_fd, (sockaddr *) & peeraddr, & socklen );
If you can normally access the connection from the Java end, you can use standard Io read/write operations to read and write data.
This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2011-11/47588.htm