C客户端与Android服务端的Socket同步通信Word格式.docx

上传人:b****5 文档编号:17479163 上传时间:2022-12-06 格式:DOCX 页数:50 大小:28.65KB
下载 相关 举报
C客户端与Android服务端的Socket同步通信Word格式.docx_第1页
第1页 / 共50页
C客户端与Android服务端的Socket同步通信Word格式.docx_第2页
第2页 / 共50页
C客户端与Android服务端的Socket同步通信Word格式.docx_第3页
第3页 / 共50页
C客户端与Android服务端的Socket同步通信Word格式.docx_第4页
第4页 / 共50页
C客户端与Android服务端的Socket同步通信Word格式.docx_第5页
第5页 / 共50页
点击查看更多>>
下载资源
资源描述

C客户端与Android服务端的Socket同步通信Word格式.docx

《C客户端与Android服务端的Socket同步通信Word格式.docx》由会员分享,可在线阅读,更多相关《C客户端与Android服务端的Socket同步通信Word格式.docx(50页珍藏版)》请在冰豆网上搜索。

C客户端与Android服务端的Socket同步通信Word格式.docx

"

adbshellambroadcast-aNotifyServiceStart"

Runtime.getRuntime().exec(

android端的代码:

ServiceBroadcastReceiver.java

packagecom.otheri.service;

importandroid.content.BroadcastReceiver;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.util.Log;

publicclassServiceBroadcastReceiverextendsBroadcastReceiver{

privatestaticStringSTART_ACTION="

NotifyServiceStart"

;

privatestaticStringSTOP_ACTION="

NotifyServiceStop"

@Override

publicvoidonReceive(Contextcontext,Intentintent){

Log.d(androidService.TAG,Thread.currentThread().getName()+"

---->

+"

ServiceBroadcastReceiveronReceive"

Stringaction=intent.getAction();

if(START_ACTION.equalsIgnoreCase(action)){

context.startService(newIntent(context,androidService.class));

ServiceBroadcastReceiveronReceivestartend"

}elseif(STOP_ACTION.equalsIgnoreCase(action)){

context.stopService(newIntent(context,androidService.class));

ServiceBroadcastReceiveronReceivestopend"

}

}

publicclassServiceBroadcastReceiverextendsBroadcastReceiver{

@Override

publicvoidonReceive(Contextcontext,Intentintent){

Log.d(androidService.TAG,Thread.currentThread().getName()+"

+"

Stringaction=intent.getAction();

if(START_ACTION.equalsIgnoreCase(action)){

context.startService(newIntent(context,androidService.class));

Log.d(androidService.TAG,Thread.currentThread().getName()+"

+"

}elseif(STOP_ACTION.equalsIgnoreCase(action)){

context.stopService(newIntent(context,androidService.class));

}

}

}

5.由于是USB连接,所以socket就可以设计为一但连接就一直联通,即在newsocket和开完out,in流后,就用个while(true){}来循环PC端和android端的读和写

android的代码:

publicvoidrun(){

aclienthasconnectedtoserver!

BufferedOutputStreamout;

BufferedInputStreamin;

try{

/*PC端发来的数据msg*/

StringcurrCMD="

out=newBufferedOutputStream(client.getOutputStream());

in=newBufferedInputStream(client.getInputStream());

//testSocket();

//测试socket方法

androidService.ioThreadFlag=true;

while(androidService.ioThreadFlag){

if(!

client.isConnected()){

break;

/*接收PC发来的数据*/

Log.v(androidService.TAG,Thread.currentThread().getName()

willread......"

/*读操作命令*/

currCMD=readCMDFromSocket(in);

**currCMD===="

+currCMD);

/*根据命令分别处理数据*/

if(currCMD.equals("

1"

)){

out.write("

OK"

.getBytes());

out.flush();

}elseif(currCMD.equals("

2"

3"

4"

/*准备接收文件数据*/

servicereceiveOK"

}catch(IOExceptione){

e.printStackTrace();

/*接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据*/

byte[]filelength=newbyte[4];

byte[]fileformat=newbyte[4];

byte[]filebytes=null;

/*从socket流中读取完整文件数据*/

filebytes=receiveFileFromSocket(in,out,filelength,

fileformat);

//Log.v(Service139.TAG,"

receivedata="

+new

//String(filebytes));

/*生成文件*/

Filefile=FileHelper.newFile("

R0013340.JPG"

FileHelper.writeFile(file,filebytes,0,

filebytes.length);

exit"

}catch(Exceptione){

//try{

//out.write("

error"

.getBytes("

utf-8"

));

//out.flush();

//}catch(IOExceptione1){

//e1.printStackTrace();

//}

Log.e(androidService.TAG,Thread.currentThread().getName()

readwriteerror111111"

out.close();

in.close();

readwriteerror222222"

}finally{

if(client!

=null){

client.close()"

client.close();

readwriteerror333333"

publicvoidrun(){

BufferedOutputStreamout;

BufferedInputStreamin;

try{

/*PC端发来的数据msg*/

StringcurrCMD="

out=newBufferedOutputStream(client.getOutputStream());

in=newBufferedInputStream(client.getInputStream());

//testSocket();

//测试socket方法

androidService.ioThreadFlag=true;

while(androidService.ioThreadFlag){

try{

if(!

client.isConnected()){

break;

}

/*接收PC发来的数据*/

Log.v(androidService.TAG,Thread.currentThread().getName()

+"

/*读操作命令*/

currCMD=readCMDFromSocket(in);

/*根据命令分别处理数据*/

if(currCMD.equals("

)){

out.write("

out.flush();

}elseif(currCMD.equals("

/*准备接收文件数据*/

try{

out.write("

out.flush();

}catch(IOExceptione){

e.printStackTrace();

}

/*接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据*/

byte[]filelength=newbyte[4];

byte[]fileformat=newbyte[4];

byte[]filebytes=null;

/*从socket流中读取完整文件数据*/

filebytes=receiveFileFromSocket(in,out,filelength,

fileformat);

//Log.v(Service139.TAG,"

+new

//String(filebytes));

/*生成文件*/

Filefile=FileHelper.newFile("

FileHelper.writeFile(file,filebytes,0,

filebytes.length);

}catch(Exceptione){

//try{

//out.write("

//out.flush();

//}catch(IOExceptione1){

//e1.printStackTrace();

//}

Log.e(androidService.TAG,Thread.currentThread().getName()

}

}

out.close();

in.close();

}catch(Exceptione){

Log.e(androidService.TAG,Thread.currentThread().getName()

e.printStackTrace();

}finally{

try{

if(client!

=null){

client.close();

}catch(IOExceptione){

Log.e(androidService.TAG,Thread.currentThread().getName()

+"

e.printStackTrace();

}

6.如果是在PC端和android端的读写操作来while(true){}循环,这样socket流的结尾不好判断,不能用“-1”来判断,因为“-1”是只有在socket关闭时才作为判断结尾。

7.socket在out.write(bytes);

时,要是数据太大时,超过socket的缓存,socket自动分包发送,所以对方就一定要用循环来多次读。

最好的办法就是服务器和客户端协议好,比如发文件时,先写过来一个要发送的文件的大小,然后再发送文件;

对方用这个大小,来循环读取数据。

android端接收数据的代码:

/**

*功能:

从socket流中读取完整文件数据

*

*InputStreamin:

socket输入流

*byte[]filelength:

流的前4个字节存储要转送的文件的字节数

*byte[]fileformat:

流的前5-8字节存储要转送的文件的格式(如.apk)

**/

publicstaticbyte[]receiveFileFromSocket(InputStreamin,

OutputStreamout,byte[]filelength,byte[]fileformat){

//文件数据

intfilelen=MyUtil.bytesToInt(filelength);

//文件长度从4字节byte[]转成Int

Stringstrtmp="

readfilelengthok:

+filelen;

out.write(strtmp.getBytes("

filebytes=newbyte[filelen];

intpos=0;

intrcvLen=0;

while((rcvLen=in.read(filebytes,pos,filelen-pos))>

0){

pos+=rcvLen;

re

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1