资源描述:
《在android系统使用socket在java层和native之间数据通信》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、在Android系统使用socket在Java层和native之间数据通信Android是基于linux的系统,系统底层机制基本上是相同的,因为分本地代码和java代码,并且是java代码通过jni调用本地代码执行。因为UDP报文发送是有局限的,局限就是当发送方的速度远远的超过接受方的时候,底层会大量的丢失数据,而且用UDP还会比较容易引起端口的阻塞,因此需要更加强大的类来实现类似的功能。LocalServerSocket和LocalSocket正是在这样的情况下产生的,是以UNIX空间为缓冲区来进行
2、数据的存储的.实例一:Native本地代码作客户端发送请求,Java端作服务器端创建socket,并监听处理socket请求:importandroid.net.LocalServerSocket;importandroid.net.LocalSocket;classSocketListenerextendsThread{@Overridepublicvoidrun(){try{LocalServerSocketserver=newLocalServerSocket("com.jenny.vnc.lo
3、calsocket");while(true){LocalSocketreceiver=server.accept();if(receiver!=null){InputStreaminput=receiver.getInputStream();intreaded=input.read();...........//handleotherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOffi
4、ceinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,Dire
5、ctorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand}}server.close();}catch(IOExceptione){Log.e(getClass().getName(),e.getMessage());}}}JNI的Native端代码做客户端:#includevoidsend_remote_request(char*
6、msg){intlocalsocket,len;structsockaddr_unremote;if((localsocket=socket(AF_UNIX,SOCK_STREAM,0))==-1){exit(1);}char*name="com.jenny.vnc.localsocket";//与java上层相同哦remote.sun_path[0]=' ';/*abstractnamespace*/strcpy(remote.sun_path+1,name);remote.sun_family=
7、AF_UNIX;intnameLen=strlen(name);len=1+nameLen+offsetof(structsockaddr_un,sun_path);if(connect(localsocket,(structsockaddr*)&remote,len)==-1){return;}if(send(localsocket,msg,strlen(msg),0)==-1){return;}otherstaffoftheCentre.Duringthewar,Zhuwastransferred
8、backtoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFe