Android native c java for local socket communication

Source: Internet
Author: User

<Pre name = "code" class = "html"> Method 1: java is used as the server and native is used as the client.
1. Create a java application and a Server class
<Pre name = "code" class = "html"> /*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License ");
* You may not use this file before t in compliance with the License.
* You may obtain a copy of the License
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "as is" BASIS,
* Without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/
Package com. example. hellojni;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android.net. LocalServerSocket;
Import android.net. LocalSocket;
 
 
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. OutputStream;
 
 
Public class HelloJni extends Activity
{
Public static final String SOCKET_NAME = "server_test ";
Private static final String TAG = "SocketService ";
Private LocalServerSocket mServerSocket = null;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
 
Log. v (TAG, "onCreate ");
Try {
MServerSocket = new LocalServerSocket (SOCKET_NAME );
} Catch (IOException e ){
Log. v (TAG, "in onCreate, making server socket:" + e );
Return;
}
 
Thread t = new Thread (){
@ Override public void run (){
LocalSocket socket = null;
While (true ){
Try {
Log. v (TAG, "Waiting for connection ...");
Socket = mServerSocket. accept ();
Log. v (TAG, "... Got socket:" + socket );
If (socket! = Null ){
StartEchoThread (socket );
} Else {
Return; // socket shutdown?
}
} Catch (IOException e ){
Log. v (TAG, "in accept:" + e );
}
}
}
};
T. start ();
 
 
}
 
Private void startEchoThread (final LocalSocket socket ){
Thread t = new Thread (){
@ Override public void run (){
Try {
InputStream is = socket. getInputStream ();
OutputStream OS = socket. getOutputStream ();
InputStreamReader isr = new InputStreamReader (is );
While (true ){
Char [] data = new char [128];
 
Int ret = isr. read (data );
For (int I = 0; I <ret; I ++ ){
Log. d (TAG, "data [" + I + "] =" + data [I]);
}
Byte [] values = TypeUtils. float2Byte (-1234567.122f );
Float fd =-1234567.122f;
Log. d (TAG, "fd =" + fd );
For (int I = 0; I <values. length; I ++ ){
Log. d (TAG, "values [" + I + "] =" + values [I]);
}
OS. write (values );
OS. flush ();
Log. v (TAG, "after write :");
}
} Catch (IOException e ){
Log. v (TAG, "in echo thread loop:" + e. getMessage ());
}
}
};
T. start ();
}
 
}


2. Tool class for converting float into byte [] Array

[Html]
Import java. nio. ByteBuffer;
Import java. nio. FloatBuffer;
 
Public class TypeUtils {
 
Public static byte [] floatToByte (float v ){
ByteBuffer bb = ByteBuffer. allocate (4 );
Byte [] ret = new byte [4];
FloatBuffer fb = bb. asFloatBuffer ();
Fb. put (v );
Bb. get (ret );
Return ret;
}
 
Public static byte [] float2Byte (float f ){
Byte [] B = new byte [4];
Int l = Float. floatToIntBits (f );
For (int I = 0; I <B. length; I ++ ){
B [I] = new Integer (l). byteValue ();
Ll = l> 8;
}
Return B;
}
 
Public static byte [] doubleToByte (double d ){
Byte [] B = new byte [8];
Long l = Double. doubleToLongBits (d );
For (int I = 0; I <B. length; I ++ ){
B [I] = new Long (l). byteValue ();
Ll = l> 8;
}
Return B;
}
 
Public static float byteToFloat (byte [] v ){
ByteBuffer bb = ByteBuffer. wrap (v );
FloatBuffer fb = bb. asFloatBuffer ();
Return fb. get ();
}
 
Public static float byte2Float (byte [] B ){
Int l = 0;
L = B [0];
L & = 0xff;
L | = (int) B [1] <8 );
L & = 0 xffff;
L | = (int) B [2] <16 );
L & = 0 xffffff;
L | = (int) B [3] <24 );
L & = 0 xffffffffl;
Return Float. intBitsToFloat (l );
}
}

3. Create a client in native
[Html]
# Include <cutils/sockets. h>
 
Static union FloatValue {
Char val [4];
Float f;
} Mf_t;
Static _ inline _ int
Qemud_fd_write (int fd, const void * buff, int len)
{
Int len2;
Do {
Len2 = write (fd, buff, len );
} While (len2 <0 & errno = EINTR );
Return len2;
}
 
Static _ inline _ int
Qemud_fd_read (int fd, void * buff, int len)
{
Int len2;
Do {
Len2 = read (fd, buff, len );
} While (len2 <0 & errno = EINTR );
Return len2;
}
 
 
 
 
Int main (int argc, char ** argv)
{
Int fd;
Char answer [200];
Char name [5] = "test! ";
Int namelen = 5;
/* Connect to qemud control socket */
Fd = socket_local_client ("server_test ",
ANDROID_SOCKET_NAMESPACE_ABSTRACT,
SOCK_STREAM );
If (fd <0 ){
Printf ("no qemud control socket: % s \ n", strerror (errno ));
Return-1;
}
 
/* Send service name to connect */
If (qemud_fd_write (fd, name, namelen )! = Namelen ){
Printf ("can't send service name to qemud: % s \ n ",
Strerror (errno ));
Close (fd );
Return-1;
}
Printf ("... before qemud_fd_read \ n ");
/* Read answer from daemon */
Int res = qemud_fd_read (fd, answer, 200 );
 
Printf ("... after qemud_fd_read ");
If (res ){
Printf ("connect to service through qemud res = % d answer0 = % d, answer1 = % d answer2 = % d, answer3 = % d \ n", res, answer [0], answer [1], answer [2], answer [3]);
Mf_t.val [0] = answer [0];
Mf_t.val [1] = answer [1];
Mf_t.val [2] = answer [2];
Mf_t.val [3] = answer [3];
Printf ("... after convert f = % f \ n", mf_t.f );
Close (fd );
Return-1;
}
 
Return 0;
}

In this way, java can communicate with native, and it is a local socket.


From weidawei0609
 

Related Article

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.