PC客户端与Android服务端的Socket同步通信

上传人:妈**** 文档编号:96847569 上传时间:2022-05-26 格式:DOCX 页数:38 大小:25.54KB
收藏 版权申诉 举报 下载
PC客户端与Android服务端的Socket同步通信_第1页
第1页 / 共38页
PC客户端与Android服务端的Socket同步通信_第2页
第2页 / 共38页
PC客户端与Android服务端的Socket同步通信_第3页
第3页 / 共38页
资源描述:

《PC客户端与Android服务端的Socket同步通信》由会员分享,可在线阅读,更多相关《PC客户端与Android服务端的Socket同步通信(38页珍藏版)》请在装配图网上搜索。

1、编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第38页 共38页 PC客户端与Android服务端的Socket同步通信(USB) 收藏 需求: 1.一个android端的service后台运行的程序,作为socket的服务器端;用于接收Pc client端发来的命令,来处理数据后,把结果发给PC client 2.PC端程序,作为socket的客户端,用于给android手机端发操作命令难点分析: 1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。 不过,我发现LG GW880就没有,要去下载个 2.android

2、默认手机端的IP为“127.0.0.1” 3.要想联通PC与android手机的sokcet,一定要用adb forward 来作下端口转发才能连上socket.view plaincopy to clipboardprint?Runtime.getRuntime().exec(adb forward tcp:12580 tcp:10086); Thread.sleep(3000); Runtime.getRuntime().exec(adb forward tcp:12580 tcp:10086);Thread.sleep(3000); 4.android端的service程序Install

3、到手机上容易,但是还要有方法来从PC的client端来启动手机上的service ,这个办法可以通过PC端adb命令来发一个Broastcast ,手机端再写个接收BroastcastReceive来接收这个Broastcast,在这个BroastcastReceive来启动service pc端命令: view plaincopy to clipboardprint?Runtime.getRuntime().exec( adb shell am broadcast -a NotifyServiceStart); Runtime.getRuntime().exec(adb shell am b

4、roadcast -a NotifyServiceStart); android端的代码:ServiceBroadcastReceiver.java view plaincopy to clipboardprint?package com.otheri.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class ServiceBroadcastRecei

5、ver extends BroadcastReceiver private static String START_ACTION = NotifyServiceStart; private static String STOP_ACTION = NotifyServiceStop; Override public void onReceive(Context context, Intent intent) Log.d(androidService.TAG, Thread.currentThread().getName() + - + ServiceBroadcastReceiver onRec

6、eive); String action = intent.getAction(); if (START_ACTION.equalsIgnoreCase(action) context.startService(new Intent(context, androidService.class); Log.d(androidService.TAG, Thread.currentThread().getName() + - + ServiceBroadcastReceiver onReceive start end); else if (STOP_ACTION.equalsIgnoreCase(a

7、ction) context.stopService(new Intent(context, androidService.class); Log.d(androidService.TAG, Thread.currentThread().getName() + - + ServiceBroadcastReceiver onReceive stop end); package com.otheri.service;import android.content.BroadcastReceiver;import android.content.Context;import android.conte

8、nt.Intent;import android.util.Log;public class ServiceBroadcastReceiver extends BroadcastReceiver private static String START_ACTION = NotifyServiceStart;private static String STOP_ACTION = NotifyServiceStop;Overridepublic void onReceive(Context context, Intent intent) Log.d(androidService.TAG, Thre

9、ad.currentThread().getName() + -+ ServiceBroadcastReceiver onReceive);String action = intent.getAction();if (START_ACTION.equalsIgnoreCase(action) context.startService(new Intent(context, androidService.class);Log.d(androidService.TAG, Thread.currentThread().getName() + -+ ServiceBroadcastReceiver o

10、nReceive start end); else if (STOP_ACTION.equalsIgnoreCase(action) context.stopService(new Intent(context, androidService.class);Log.d(androidService.TAG, Thread.currentThread().getName() + -+ ServiceBroadcastReceiver onReceive stop end); 5.由于是USB连接,所以socket就可以设计为一但连接就一直联通,即在new socket和开完out,in流后,就用

11、个while(true)来循环PC端和android端的读和写 android的代码:view plaincopy to clipboardprint?public void run() Log.d(androidService.TAG, Thread.currentThread().getName() + - + a client has connected to server!); BufferedOutputStream out; BufferedInputStream in; try /* PC端发来的数据msg */ String currCMD = ; out = new Buff

12、eredOutputStream(client.getOutputStream(); in = new BufferedInputStream(client.getInputStream(); / testSocket();/ 测试socket方法 androidService.ioThreadFlag = true; while (androidService.ioThreadFlag) try if (!client.isConnected() break; /* 接收PC发来的数据 */ Log.v(androidService.TAG, Thread.currentThread().g

13、etName() + - + will read.); /* 读操作命令 */ currCMD = readCMDFromSocket(in); Log.v(androidService.TAG, Thread.currentThread().getName() + - + *currCMD = + currCMD); /* 根据命令分别处理数据 */ if (currCMD.equals(1) out.write(OK.getBytes(); out.flush(); else if (currCMD.equals(2) out.write(OK.getBytes(); out.flush(

14、); else if (currCMD.equals(3) out.write(OK.getBytes(); out.flush(); else if (currCMD.equals(4) /* 准备接收文件数据 */ try out.write(service receive OK.getBytes(); out.flush(); catch (IOException e) e.printStackTrace(); /* 接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据 */ byte filelength = new byte4; byte fileformat = new b

15、yte4; byte filebytes = null; /* 从socket流中读取完整文件数据 */ filebytes = receiveFileFromSocket(in, out, filelength, fileformat); / Log.v(Service139.TAG, receive data = + new / String(filebytes); try /* 生成文件 */ File file = FileHelper.newFile(R0013340.JPG); FileHelper.writeFile(file, filebytes, 0, filebytes.l

16、ength); catch (IOException e) e.printStackTrace(); else if (currCMD.equals(exit) catch (Exception e) / try / out.write(error.getBytes(utf-8); / out.flush(); / catch (IOException e1) / e1.printStackTrace(); / Log.e(androidService.TAG, Thread.currentThread().getName() + - + read write error111111); ou

17、t.close(); in.close(); catch (Exception e) Log.e(androidService.TAG, Thread.currentThread().getName() + - + read write error222222); e.printStackTrace(); finally try if (client != null) Log.v(androidService.TAG, Thread.currentThread().getName() + - + client.close(); client.close(); catch (IOExceptio

18、n e) Log.e(androidService.TAG, Thread.currentThread().getName() + - + read write error333333); e.printStackTrace(); public void run() Log.d(androidService.TAG, Thread.currentThread().getName() + -+ a client has connected to server!);BufferedOutputStream out;BufferedInputStream in;try /* PC端发来的数据msg

19、*/String currCMD = ;out = new BufferedOutputStream(client.getOutputStream();in = new BufferedInputStream(client.getInputStream();/ testSocket();/ 测试socket方法androidService.ioThreadFlag = true;while (androidService.ioThreadFlag) try if (!client.isConnected() break;/* 接收PC发来的数据 */Log.v(androidService.T

20、AG, Thread.currentThread().getName()+ - + will read.);/* 读操作命令 */currCMD = readCMDFromSocket(in);Log.v(androidService.TAG, Thread.currentThread().getName()+ - + *currCMD = + currCMD);/* 根据命令分别处理数据 */if (currCMD.equals(1) out.write(OK.getBytes();out.flush(); else if (currCMD.equals(2) out.write(OK.ge

21、tBytes();out.flush(); else if (currCMD.equals(3) out.write(OK.getBytes();out.flush(); else if (currCMD.equals(4) /* 准备接收文件数据 */try out.write(service receive OK.getBytes();out.flush(); catch (IOException e) e.printStackTrace();/* 接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据 */byte filelength = new byte4;byte filef

22、ormat = new byte4;byte filebytes = null;/* 从socket流中读取完整文件数据 */filebytes = receiveFileFromSocket(in, out, filelength,fileformat);/ Log.v(Service139.TAG, receive data = + new/ String(filebytes);try /* 生成文件 */File file = FileHelper.newFile(R0013340.JPG);FileHelper.writeFile(file, filebytes, 0,filebyte

23、s.length); catch (IOException e) e.printStackTrace(); else if (currCMD.equals(exit) catch (Exception e) / try / out.write(error.getBytes(utf-8);/ out.flush();/ catch (IOException e1) / e1.printStackTrace();/ Log.e(androidService.TAG, Thread.currentThread().getName()+ - + read write error111111);out.

24、close();in.close(); catch (Exception e) Log.e(androidService.TAG, Thread.currentThread().getName()+ - + read write error222222);e.printStackTrace(); finally try if (client != null) Log.v(androidService.TAG, Thread.currentThread().getName()+ - + client.close();client.close(); catch (IOException e) Lo

25、g.e(androidService.TAG, Thread.currentThread().getName()+ - + read write error333333);e.printStackTrace(); 6.如果是在PC端和android端的读写操作来while(true)循环,这样socket流的结尾不好判断,不能用“-1”来判断,因为“-1”是只有在socket关闭时才作为判断结尾。 7.socket在out.write(bytes);时,要是数据太大时,超过socket的缓存,socket自动分包发送,所以对方就一定要用循环来多次读。最好的办法就是服务器和客户端协议好,比如发文

26、件时,先写过来一个要发送的文件的大小,然后再发送文件;对方用这个大小,来循环读取数据。 android端接收数据的代码:view plaincopy to clipboardprint?/* * 功能:从socket流中读取完整文件数据 * * InputStream in:socket输入流 * * byte filelength: 流的前4个字节存储要转送的文件的字节数 * * byte fileformat:流的前5-8字节存储要转送的文件的格式(如.apk) * * */ public static byte receiveFileFromSocket(InputStream in,

27、OutputStream out, byte filelength, byte fileformat) byte filebytes = null;/ 文件数据 try int filelen = MyUtil.bytesToInt(filelength);/ 文件长度从4字节byte转成Int String strtmp = read file length ok: + filelen; out.write(strtmp.getBytes(utf-8); out.flush(); filebytes = new bytefilelen; int pos = 0; int rcvLen = 0

28、; while (rcvLen = in.read(filebytes, pos, filelen - pos) 0) pos += rcvLen; Log.v(androidService.TAG, Thread.currentThread().getName() + - + read file OK:file size= + filebytes.length); out.write(read file ok.getBytes(utf-8); out.flush(); catch (Exception e) Log.v(androidService.TAG, Thread.currentTh

29、read().getName() + - + receiveFileFromSocket error); e.printStackTrace(); return filebytes; /* * 功能:从socket流中读取完整文件数据 * * InputStream in:socket输入流 * * byte filelength: 流的前4个字节存储要转送的文件的字节数 * * byte fileformat:流的前5-8字节存储要转送的文件的格式(如.apk) * * */public static byte receiveFileFromSocket(InputStream in,Out

30、putStream out, byte filelength, byte fileformat) byte filebytes = null;/ 文件数据try int filelen = MyUtil.bytesToInt(filelength);/ 文件长度从4字节byte转成IntString strtmp = read file length ok: + filelen;out.write(strtmp.getBytes(utf-8);out.flush();filebytes = new bytefilelen;int pos = 0;int rcvLen = 0;while (rc

31、vLen = in.read(filebytes, pos, filelen - pos) 0) pos += rcvLen;Log.v(androidService.TAG, Thread.currentThread().getName()+ - + read file OK:file size= + filebytes.length);out.write(read file ok.getBytes(utf-8);out.flush(); catch (Exception e) Log.v(androidService.TAG, Thread.currentThread().getName(

32、)+ - + receiveFileFromSocket error);e.printStackTrace();return filebytes; 8.socket的最重要的机制就是读写采用的是阻塞的方式,如果客户端作为命令发起者,服务器端作为接收者的话,只有当客户端client用out.writer()写到输出流里后,即流中有数据service的read才会执行,不然就会一直停在read()那里等数据。 9.还要让服务器端可以同时连接多个client,即服务器端用new thread()来作数据读取操作。源码:客户端(pc端):testPcClient.javaview plaincopy

33、to clipboardprint?import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import .InetAddress; import .Socket; import .Unkno

34、wnHostException; public class testPcClient /* * param args * throws InterruptedException */ public static void main(String args) throws InterruptedException try Runtime.getRuntime().exec( adb shell am broadcast -a NotifyServiceStop); Thread.sleep(3000); Runtime.getRuntime().exec(adb forward tcp:1258

35、0 tcp:10086); Thread.sleep(3000); Runtime.getRuntime().exec( adb shell am broadcast -a NotifyServiceStart); Thread.sleep(3000); catch (IOException e3) e3.printStackTrace(); Socket socket = null; try InetAddress serverAddr = null; serverAddr = InetAddress.getByName(127.0.0.1); System.out.println(TCP

36、1111 + C: Connecting.); socket = new Socket(serverAddr, 12580); String str = hi,wufenglong; System.out.println(TCP 221122 + C:RECEIVE); BufferedOutputStream out = new BufferedOutputStream(socket .getOutputStream(); BufferedInputStream in = new BufferedInputStream(socket .getInputStream(); BufferedRe

37、ader br = new BufferedReader(new InputStreamReader( System.in); boolean flag = true; while (flag) System.out.print(请输入16的数字,退出输入exit:); String strWord = br.readLine();/ 从控制台输入16 if (strWord.equals(1) out.write(1.getBytes(); out.flush(); System.out.println(1 finish sending the data); String strFormso

38、cket = readFromSocket(in); System.out.println(the data sent by server is:rn + strFormsocket); System.out .println(=); else if (strWord.equals(2) out.write(2.getBytes(); out.flush(); System.out.println(2 finish sending the data); String strFormsocket = readFromSocket(in); System.out.println(the data

39、sent by server is:rn + strFormsocket); System.out .println(=); else if (strWord.equals(3) out.write(3.getBytes(); out.flush(); System.out.println(3 finish sending the data); String strFormsocket = readFromSocket(in); System.out.println(the data sent by server is:rn + strFormsocket); System.out .prin

40、tln(=); else if (strWord.equals(4) /* 发送命令 */ out.write(4.getBytes(); out.flush(); System.out.println(send file finish sending the CMD:); /* 服务器反馈:准备接收 */ String strFormsocket = readFromSocket(in); System.out .println(service ready receice data:UPDATE_CONTACTS: + strFormsocket); byte filebytes = Fil

41、eHelper.readFile(R0013340.JPG); System.out.println(file size= + filebytes.length); /* 将整数转成4字节byte数组 */ byte filelength = new byte4; filelength = tools.intToByte(filebytes.length); /* 将.apk字符串转成4字节byte数组 */ byte fileformat = null; fileformat = .apk.getBytes(); System.out .println(fileformat length=

42、+ fileformat.length); /* 字节流中前4字节为文件长度,4字节文件格式,以后是文件流 */ /* 注意如果write里的byte超过socket的缓存,系统自动分包写过去,所以对方要循环写完 */ out.write(filelength); out.flush(); String strok1 = readFromSocket(in); System.out.println(service receive filelength : + strok1); / out.write(fileformat); / out.flush(); / String strok2 = readFromSocket(in); / System.out.println(service receive fileformat : + / strok2); System.out.println(write data to android); out.write(filebytes); out.flush(); System.out.println(*); /* 服务器反馈:接收成功 */ String strread = readFromSocket(in); System.out.

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!