android native c java进行本地socket通信

  1. <PRE class=html name="code" sizcache="1" sizset="2">方式一:java做服务器端,native做client端
  2. 1. 建立java应用程序,建立Server 类
  3. <PRE class=html name="code">/*
  4. * Copyright (C) 2009 The Android Open Source Project
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.example.hellojni;
  19. import android.app.Activity;
  20. import android.os.Bundle;
  21. import android.util.Log;
  22. import android.net.LocalServerSocket;
  23. import android.net.LocalSocket;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.io.OutputStream;
  28. public class HelloJni extends Activity
  29. {
  30. public static final String SOCKET_NAME = "server_test";
  31. private static final String TAG = "SocketService";
  32. private LocalServerSocket mServerSocket = null;
  33. /** Called when the activity is first created. */
  34. @Override
  35. public void onCreate(Bundle savedInstanceState)
  36. {
  37. super.onCreate(savedInstanceState);
  38. Log.v(TAG, "onCreate");
  39. try {
  40. mServerSocket = new LocalServerSocket(SOCKET_NAME);
  41. } catch (IOException e) {
  42. Log.v(TAG, "in onCreate, making server socket: " + e);
  43. return;
  44. }
  45. Thread t = new Thread() {
  46. @Override public void run() {
  47. LocalSocket socket = null;
  48. while (true) {
  49. try {
  50. Log.v(TAG, "Waiting for connection...");
  51. socket = mServerSocket.accept();
  52. Log.v(TAG, ".....Got socket: " + socket);
  53. if (socket != null) {
  54. startEchoThread(socket);
  55. } else {
  56. return; // socket shutdown?
  57. }
  58. } catch (IOException e) {
  59. Log.v(TAG, "in accept: " + e);
  60. }
  61. }
  62. }
  63. };
  64. t.start();
  65. }
  66. private void startEchoThread(final LocalSocket socket) {
  67. Thread t = new Thread() {
  68. @Override public void run() {
  69. try {
  70. InputStream is = socket.getInputStream();
  71. OutputStream os = socket.getOutputStream();
  72. InputStreamReader isr = new InputStreamReader(is);
  73. while (true) {
  74. char[] data = new char[128];
  75. int ret = isr.read(data);
  76. for(int i=0;i<ret;i++){
  77. Log.d(TAG, "data["+i+"]="+data[i]);
  78. }
  79. byte[] values = TypeUtils.float2Byte(-1234567.122f);
  80. float fd = -1234567.122f;
  81. Log.d(TAG, " fd="+fd);
  82. for(int i=0;i<values.length;i++){
  83. Log.d(TAG, "values["+i+"]="+values[i]);
  84. }
  85. os.write(values);
  86. os.flush();
  87. Log.v(TAG, "after write: ");
  88. }
  89. } catch (IOException e) {
  90. Log.v(TAG, "in echo thread loop: " + e.getMessage());
  91. }
  92. }
  93. };
  94. t.start();
  95. }
  96. }</PRE><BR>
  97. <BR>
  98. <PRE></PRE>
  99. <PRE></PRE>
  100. <P>2.将float转换成byte[]数组的工具类</P>
  101. <P></P>
  102. <PRE class=html name="code">import java.nio.ByteBuffer;
  103. import java.nio.FloatBuffer;
  104. public class TypeUtils {
  105. public static byte[] floatToByte(float v) {
  106. ByteBuffer bb = ByteBuffer.allocate(4);
  107. byte[] ret = new byte[4];
  108. FloatBuffer fb = bb.asFloatBuffer();
  109. fb.put(v);
  110. bb.get(ret);
  111. return ret;
  112. }
  113. public static byte[] float2Byte(float f) {
  114. byte[] b = new byte[4];
  115. int l = Float.floatToIntBits(f);
  116. for (int i = 0; i < b.length; i++) {
  117. b[i] = new Integer(l).byteValue();
  118. l = l >> 8;
  119. }
  120. return b;
  121. }
  122. public static byte[] doubleToByte(double d) {
  123. byte[] b = new byte[8];
  124. long l = Double.doubleToLongBits(d);
  125. for (int i = 0; i < b.length; i++) {
  126. b[i] = new Long(l).byteValue();
  127. l = l >> 8;
  128. }
  129. return b;
  130. }
  131. public static float byteToFloat(byte[] v) {
  132. ByteBuffer bb = ByteBuffer.wrap(v);
  133. FloatBuffer fb = bb.asFloatBuffer();
  134. return fb.get();
  135. }
  136. public static float byte2Float(byte[] b) {
  137. int l = 0;
  138. l = b[0];
  139. l &= 0xff;
  140. l |= ((int) b[1] << 8);
  141. l &= 0xffff;
  142. l |= ((int) b[2] << 16);
  143. l &= 0xffffff;
  144. l |= ((int) b[3] << 24);
  145. l &= 0xffffffffl;
  146. return Float.intBitsToFloat(l);
  147. }
  148. }</PRE><BR>
  149. 3.在 native中建立client
  150. <P></P>
  151. <P></P>
  152. <PRE class=html name="code">#include <cutils/sockets.h>
  153. static union FloatValue{
  154. char val[4];
  155. float f;
  156. } mf_t;
  157. static __inline__ int
  158. qemud_fd_write(int fd, const void* buff, int len)
  159. {
  160. int len2;
  161. do {
  162. len2 = write(fd, buff, len);
  163. } while (len2 < 0 && errno == EINTR);
  164. return len2;
  165. }
  166. static __inline__ int
  167. qemud_fd_read(int fd, void* buff, int len)
  168. {
  169. int len2;
  170. do {
  171. len2 = read(fd, buff, len);
  172. } while (len2 < 0 && errno == EINTR);
  173. return len2;
  174. }
  175. int main(int argc, char **argv)
  176. {
  177. int fd;
  178. char answer[200];
  179. char name[5]= "test!";
  180. int namelen = 5;
  181. /* connect to qemud control socket */
  182. fd = socket_local_client( "server_test",
  183. ANDROID_SOCKET_NAMESPACE_ABSTRACT,
  184. SOCK_STREAM );
  185. if (fd < 0) {
  186. printf("no qemud control socket: %s \n", strerror(errno));
  187. return -1;
  188. }
  189. /* send service name to connect */
  190. if (qemud_fd_write(fd, name, namelen) != namelen) {
  191. printf("can't send service name to qemud: %s \n",
  192. strerror(errno));
  193. close(fd);
  194. return -1;
  195. }
  196. printf(".... before qemud_fd_read \n");
  197. /* read answer from daemon */
  198. int res =qemud_fd_read(fd, answer, 200);
  199. printf(" .....after qemud_fd_read ");
  200. if (res) {
  201. 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]);
  202. mf_t.val[0] = answer[0];
  203. mf_t.val[1] = answer[1];
  204. mf_t.val[2] = answer[2];
  205. mf_t.val[3] = answer[3];
  206. printf(" .....after convert f=%f \n",mf_t.f);
  207. close(fd);
  208. return -1;
  209. }
  210. return 0;
  211. }</PRE><BR>
  212. 这样就实现了java和native进行通信的目的了,而且是本地socket哟。<BR>
  213. <P></P>
  214. <P><BR>
  215. </P>
  216. <PRE></PRE>
  217. <PRE></PRE>
  218. <PRE></PRE>
  219. <PRE></PRE>
  220. <PRE></PRE>
  221. <PRE></PRE>
  222. </PRE>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值