android的NDK和java进行本地socket通信
来源:互联网 发布:文件上传java原理 编辑:程序博客网 时间:2024/05/22 10:37
android的NDK和java进行本地socket通信
本文主要列出android的NDK和java进行本地socket通信的核心代码,啥话也不说了,直接列出android的NDK和java进行本地socket通信的代码,仅供参考:
1.编写socket服务端代码,生成可执行脚本htfsk。
1 #define SOCKET_NAME "htfsk"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <netinet/in.h>
9 #include <sys/socket.h>
10 #include <sys/wait.h>
11 #include <sys/un.h>
12 #include <cutils/sockets.h>
13 #include <utils/Log.h>
14 #include <android/log.h>
15
16
17 int main(){
18 char log[200];
19
20 int connect_number = 6;
21 int fdListen = -1, new_fd = -1;
22 int ret;
23 struct sockaddr_un peeraddr;
24 socklen_t socklen = sizeof (peeraddr);
25 int numbytes ;
26 char buff[256];
27 //这一步很关键,就是获取init.rc中配置的名为 "htfsk" 的socket
28 fdListen = android_get_control_socket(SOCKET_NAME);
29 if (fdListen < 0) {
30 sprintf(log,"Failed to get socket '" SOCKET_NAME "' errno:%d", errno);
31 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
32 exit(-1);
33 }
34 //开始监听
35 ret = listen(fdListen, connect_number);
36
37 sprintf(log,"Listen result %d",ret);
38 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
39
40 if (ret < 0) {
41 perror("listen");
42 exit(-1);
43 }
44 //等待Socket客户端发启连接请求
45 new_fd = accept(fdListen, (struct sockaddr *) &peeraddr, &socklen);
46 sprintf(log,"Accept_fd %d",new_fd);
47 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
48 if (new_fd < 0 ) {
49 sprintf(log,"%d",errno);
50 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
51 perror("accept error");
52 exit(-1);
53 }
54
55 while(1){
56 //循环等待Socket客户端发来消息
57 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI","Waiting for receive");
58 if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1){
59 sprintf(log,"%d",errno);
60 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
61 perror("recv");
62 continue;
63 }
64 //发送消息回执给Socket客户端
65 if(send(new_fd,buff,strlen(buff),0)==-1)
66 {
67 perror("send");
68 close(new_fd);
69 exit(0);
70 }
71 }
72
73 close(new_fd);
74 close(fdListen);
75 return 0;
76 }
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <netinet/in.h>
9 #include <sys/socket.h>
10 #include <sys/wait.h>
11 #include <sys/un.h>
12 #include <cutils/sockets.h>
13 #include <utils/Log.h>
14 #include <android/log.h>
15
16
17 int main(){
18 char log[200];
19
20 int connect_number = 6;
21 int fdListen = -1, new_fd = -1;
22 int ret;
23 struct sockaddr_un peeraddr;
24 socklen_t socklen = sizeof (peeraddr);
25 int numbytes ;
26 char buff[256];
27 //这一步很关键,就是获取init.rc中配置的名为 "htfsk" 的socket
28 fdListen = android_get_control_socket(SOCKET_NAME);
29 if (fdListen < 0) {
30 sprintf(log,"Failed to get socket '" SOCKET_NAME "' errno:%d", errno);
31 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
32 exit(-1);
33 }
34 //开始监听
35 ret = listen(fdListen, connect_number);
36
37 sprintf(log,"Listen result %d",ret);
38 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
39
40 if (ret < 0) {
41 perror("listen");
42 exit(-1);
43 }
44 //等待Socket客户端发启连接请求
45 new_fd = accept(fdListen, (struct sockaddr *) &peeraddr, &socklen);
46 sprintf(log,"Accept_fd %d",new_fd);
47 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
48 if (new_fd < 0 ) {
49 sprintf(log,"%d",errno);
50 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
51 perror("accept error");
52 exit(-1);
53 }
54
55 while(1){
56 //循环等待Socket客户端发来消息
57 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI","Waiting for receive");
58 if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1){
59 sprintf(log,"%d",errno);
60 __android_log_write(ANDROID_LOG_DEBUG,"FTM_JNI",log);
61 perror("recv");
62 continue;
63 }
64 //发送消息回执给Socket客户端
65 if(send(new_fd,buff,strlen(buff),0)==-1)
66 {
67 perror("send");
68 close(new_fd);
69 exit(0);
70 }
71 }
72
73 close(new_fd);
74 close(fdListen);
75 return 0;
76 }
2.编写客户端java代码。核心代码如下:
1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.io.PrintWriter;
5
6 import android.net.LocalSocket;
7 import android.net.LocalSocketAddress;
8 import android.util.Log;
9
10
11 public class SocketClient {
12 private final String SOCKET_NAME = "htfsk";
13 private LocalSocket client;
14 private LocalSocketAddress address;
15 private boolean isConnected = false;
16 private int connetTime = 1;
17
18 public SocketClient() {
19 client = new LocalSocket();
20 address = new LocalSocketAddress(SOCKET_NAME, LocalSocketAddress.Namespace.RESERVED);
21 new ConnectSocketThread().start();
22 }
23
24 /**
25 * 发送消息
26 * @param msg
27 * @return 返回Socket服务端的消息回执
28 */
29 public String sendMsg(String msg) {
30 if (!isConnected) {
31 return "Connect fail";
32 }
33 try {
34 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
35 PrintWriter out = new PrintWriter(client.getOutputStream());
36 out.println(msg);
37 out.flush();
38 return in.readLine();
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42 return "Nothing return";
43 }
44
45 /**
46 * 异步连接Socket,如果连接不上会尝试重复连接十次
47 *
48 * @author Administrator
49 *
50 */
51 private class ConnectSocketThread extends Thread {
52 @Override
53 public void run() {
54 while (!isConnected && connetTime <= 10) {
55 try {
56 sleep(1000);
57 Log.i("SocketClient","Try to connect socket;ConnectTime:"+connetTime);
58 client.connect(address);
59 isConnected = true;
60 } catch (Exception e) {
61 connetTime++;
62 isConnected = false;
63 Log.i("SocketClient","Connect fail");
64 }
65 }
66 }
67 }
68
69 /**
70 * 关闭Socket
71 */
72 public void closeSocket() {
73 try {
74 client.close();
75 } catch (IOException e) {
76 e.printStackTrace();
77 }
78 }
79 }
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.io.PrintWriter;
5
6 import android.net.LocalSocket;
7 import android.net.LocalSocketAddress;
8 import android.util.Log;
9
10
11 public class SocketClient {
12 private final String SOCKET_NAME = "htfsk";
13 private LocalSocket client;
14 private LocalSocketAddress address;
15 private boolean isConnected = false;
16 private int connetTime = 1;
17
18 public SocketClient() {
19 client = new LocalSocket();
20 address = new LocalSocketAddress(SOCKET_NAME, LocalSocketAddress.Namespace.RESERVED);
21 new ConnectSocketThread().start();
22 }
23
24 /**
25 * 发送消息
26 * @param msg
27 * @return 返回Socket服务端的消息回执
28 */
29 public String sendMsg(String msg) {
30 if (!isConnected) {
31 return "Connect fail";
32 }
33 try {
34 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
35 PrintWriter out = new PrintWriter(client.getOutputStream());
36 out.println(msg);
37 out.flush();
38 return in.readLine();
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42 return "Nothing return";
43 }
44
45 /**
46 * 异步连接Socket,如果连接不上会尝试重复连接十次
47 *
48 * @author Administrator
49 *
50 */
51 private class ConnectSocketThread extends Thread {
52 @Override
53 public void run() {
54 while (!isConnected && connetTime <= 10) {
55 try {
56 sleep(1000);
57 Log.i("SocketClient","Try to connect socket;ConnectTime:"+connetTime);
58 client.connect(address);
59 isConnected = true;
60 } catch (Exception e) {
61 connetTime++;
62 isConnected = false;
63 Log.i("SocketClient","Connect fail");
64 }
65 }
66 }
67 }
68
69 /**
70 * 关闭Socket
71 */
72 public void closeSocket() {
73 try {
74 client.close();
75 } catch (IOException e) {
76 e.printStackTrace();
77 }
78 }
79 }
0 0
- android的NDK和java进行本地socket通信
- android的NDK和java进行本地socket通信
- android的NDK和java进行本地socket通信
- android的NDK和java进行本地socket通信-----C服务端,java客户端
- android native c java进行本地socket通信
- android native c java进行本地socket通信
- android native c java进行本地socket通信
- android使用socket于本地PC进行通信的问题
- android native c java进行本地socket通信----java服务端 C客户端
- android/java 与 c#进行socket通信
- vc++ 与 android/java 进行socket通信
- C++和android进行socket通信小结
- c 和 java 通过 socket 进行通信
- Java Socket进行客户端和服务器进行通信的简单例子
- Android java和C的Socket通信demo(可用)
- Matlab探寻(二):和Android进行TCPIP通信(java.net.Socket)
- Socket本地通信和网络通信
- Android使用Socket进行通信
- 获取被人电话本的小恶意程序
- 面试中的一些问题——JAVA(二)
- POJ3620 Avoid The Lakes
- IO转换流
- Python基本数据类型
- android的NDK和java进行本地socket通信
- 不容易系列之一
- Problem A
- BZOJ3876: [Ahoi2014]支线剧情
- 视频编解码学习之一:理论基础
- HBase总结(9)--计数器
- CodeForces 624B Making a String
- Archlinux安装pip
- “Accelerated C++” 阅读笔记(一)