其实,我已经在android模拟器(ps:它们都运行成功,并与android genric交叉编译器编译)server.c和client.c做了testing。然后,我进一步,我重写客户端而通过jni.But,在这种情况下,客户端无法连接到服务器端,虽然新的客户端是非常类似于client.c。 但是,当我添加<uses-permission android:name="android.permission.INTERNET" /> (ps:这个标签在应用程序标签之外)时,问题仍然出现。 正如logcat所显示的那样,java代码实际上是调用c方法,但为什么不像client.c那样? 任何想法都会使我受益匪浅。提前感谢!
server.c:
/* Make the necessary includes and set up the variables. */ #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> int main() { int server_sockfd,client_sockfd; int server_len,client_len; struct sockaddr_un server_address; struct sockaddr_un client_address; /* Remove any old socket and create an unnamed socket for the server. */ unlink("server_socket"); server_sockfd = socket(AF_UNIX,SOCK_STREAM,0); /* Name the socket. */ server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path,"server_socket"); server_len = sizeof(server_address); bind(server_sockfd,(struct sockaddr *)&server_address,server_len); /* Create a connection queue and wait for clients. */ listen(server_sockfd,5); while(1) { char ch; printf("server waitingn"); /* Accept a connection. */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,&client_len); /* We can Now read/write to client on client_sockfd. */ read(client_sockfd,&ch,1); ch++; write(client_sockfd,1); close(client_sockfd); } }
client.c:
/* Make the necessary includes and set up the variables. */ #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> int main() { int sockfd; int len; struct sockaddr_un address; int result; char ch = 'A'; /* Create a socket for the client. */ sockfd = socket(AF_UNIX,0); /* Name the socket,as agreed with the server. */ address.sun_family = AF_UNIX; strcpy(address.sun_path,"server_socket"); len = sizeof(address); /* Now connect our socket to the server's socket. */ result = connect(sockfd,(struct sockaddr *)&address,len); if(result == -1) { perror("oops: client1"); exit(1); } /* We can Now read/write via sockfd. */ write(sockfd,1); read(sockfd,1); printf("char from server = %cn",ch); close(sockfd); exit(0); }
java代码:package gz.kaiwii;
如何通过android中的应用程序运行多个shell命令
如何在Android和电脑之间发送/接收文本?
bash:/ usr / local / android-sdk-linux / tools / android:权限被拒绝
Android在Ubuntu上失败
用input模拟多点触摸事件
public class NSocket { static{ System.loadLibrary("NSocket"); } public native void start(); }
本地代码:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> #include <android/log.h> #include <android/bitmap.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> #define LOG_TAG "NSocket" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,__VA_ARGS__) JNIEXPORT void JNICALL Java_gz_kaiwii_NSocket_start (jnienv * env,jobject object){ LOGI("JNICALL Java_gz_kaiwii_NSocket_start is called!"); int sockfd; int len; struct sockaddr_un address; int result; char ch = 'A'; /* Create a socket for the client. */ LOGI(" Create a socket for the client!"); sockfd = socket(AF_UNIX,0); if(sockfd==-1){ LOGE("create socket error!!!!!"); } /* Name the socket,len); LOGI(" Now connect our socket to the server's socket."); if(result == -1) { LOGE("connect error!"); exit(1); } /* We can Now read/write via sockfd. */ write(sockfd,1); /* printf("char from server = %cn",ch); */ LOGI("char from server = %cn",ch); close(sockfd); }
logcat:
Android Studio在运行OneDrive的计算机上不断生成错误位置的.android文件夹
Android Studio + DDMS错误“推送select失败:只读文件系统”
与Windows 10和Android的WiFi直接P2P服务发现 – 可能吗?
如何在Windows 8中安装瓢虫的android?
如果我使用抽象名称空间作为名称,一切都运行正常。但是,有一点要注意的是长度!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。