PC客戶端與Android服務(wù)端的Socket同步通信USB_第1頁(yè)
PC客戶端與Android服務(wù)端的Socket同步通信USB_第2頁(yè)
PC客戶端與Android服務(wù)端的Socket同步通信USB_第3頁(yè)
PC客戶端與Android服務(wù)端的Socket同步通信USB_第4頁(yè)
PC客戶端與Android服務(wù)端的Socket同步通信USB_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余21頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、http:/ 客戶端與 Android 服務(wù)端的 Socket 同步通信(USB)分類:android2010-07-3117:238027 人閱讀評(píng)論(52)收藏舉報(bào)需求:1 .一個(gè) android 端的 service 后臺(tái)運(yùn)行的程序,作為 socket 的服務(wù)器端;用于接收 Pcclient 端發(fā)來(lái)的命令,來(lái)處理數(shù)據(jù)后,把 Z果發(fā)給 PCclient2.PC 端程序,作為 socket 的客戶端,用于給 android 手機(jī)端發(fā)操作命令難點(diǎn)分析:1 .手機(jī)一定要有 adb 模式,即插上 USB 線時(shí)馬上提示白對(duì)話框選 adb。好多對(duì)手機(jī)的操作都可以用 adb 直接作。不過(guò),我發(fā)現(xiàn) LGG

2、W880 就沒(méi)有,要去下載個(gè)2 .android 默認(rèn)手機(jī)端的 IP 為“”3 .要想聯(lián)通 PC 與 android 手機(jī)的 sokcet,一定要用 adbforward 來(lái)作下端口轉(zhuǎn)發(fā)才能連上 socket.viewplaincopytoclipboardprint?1.Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);2.Thread.sleep(3000);Runtime.getRuntime().exec(adbforwardtcp:12580tcThread.sleep(3000);一4.android

3、端的 service 程序 Install 到手機(jī)上容易,但是還要有方法來(lái)從 PC 的 client 端來(lái)啟動(dòng)手機(jī)上的 service,這個(gè)辦法可以通過(guò)PC 端 adb 命令來(lái)發(fā)一個(gè) Broastcast,手機(jī)端再寫個(gè)接收 BroastcastReceive 來(lái)接收這個(gè) Broastcast,在這個(gè) BroastcastReceive 來(lái)啟動(dòng)servicepc 端命令:viewplaincopytoclipboardprint?1.Runtime.getRuntime().exec(2.adbshellambroadcast-aNotifyServiceStart);Runtime.getRu

4、ntime().exec(adbshellambroadcast-aNotifyServiceStart);illandroid 端的代碼:ServiceBroadcastReceiver.javaviewplaincopytoclipboardprint?1.packagecom.otheri.service;2.3.importandroid.content.BroadcastReceiver;4.importandroid.content.Context;5.importandroid.content.Intent;6.importandroid.util.Log;7.8.publicc

5、lassServiceBroadcastReceiverextendsBroadcastReceiver9.privatestaticStringSTART_ACTION=NotifyServiceStart;10.privatestaticStringSTOP_ACTION=NotifyServiceStop;11.12.Override13.publicvoidonReceive(Contextcontext,Intentintent)Log.d(androidService.TAG,Thread.currentThread().getName()+14.15.+ServiceBroadc

6、astReceiveronReceive);16.17.Stringaction=intent.getAction();18.if(START_ACTION.equalsIgnoreCase(action)19.context.startService(newIntent(context,androidService.class);20.21.Log.d(androidService.TAG,Thread.currentThread().getName()+-22.+ServiceBroadcastReceiveronReceivestartend);23.elseif(STOP_ACTION

7、.equalsIgnoreCase(action)24.context.stopService(newIntent(context,androidService.class);25.Log.d(androidService.TAG,Thread.currentThread().getName()+-26.+ServiceBroadcastReceiveronReceivestopend);0.packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Cont

8、ext;importandroid.content.Intent;彘importandroid.util.Log;publicclassServiceBroadcastReceiverextendsBroad蠲privatestaticStringSTART_ACTION=NotifprivatestaticStringSTOP_ACTION=Notify踮圖,Override理堀publicvoidonReceive(Contextcontext,Inte機(jī)翻Log.d(androidService.TAG,Thread.cu+ServiceBroadcast,明Stringaction=i

9、ntent.getAction();if(START_ACTION.equalsIgnoreCase(a刀5.由于是 USB 連接,所以 socket 就可以設(shè)計(jì)為一但連接就一直聯(lián)通,即在 newsocket 和開(kāi)完 out,in 流后,就用個(gè)while(true)來(lái)循環(huán) PC 端和 android 端的讀和寫android 的代碼:viewplaincopytoclipboardprint?1.publicvoidrun()2.Log.d(androidService.TAG,Thread.currentThread().getName()+-3.+aclienthasconnectedto

10、server!);4.BufferedOutputStreamout;5.BufferedInputStreamin;7./*PC 端發(fā)來(lái)的數(shù)據(jù) msg*/8.StringcurrCMD=;9.out=newBufferedOutputStream(client.getOutputStream();10.in=newBufferedInputStream(client.getInputStream();11./testSocket();/測(cè)試 socket 方法12.androidService.ioThreadFlag=true;13.while(androidService.ioThrea

11、dFlag)14.try15.if(!client.isConnected()16.break;17.18.19./*接收 PC 發(fā)來(lái)的數(shù)據(jù)*/20.Log.v(androidService.TAG,Thread.currentThread().getName()21.+-+willread.);22./*讀操作命令*/23.currCMD=readCMDFromSocket(in);24.Log.v(androidService.TAG,Thread.currentThread().getName()25.+-+*currCMD=+currCMD);26.27./*根據(jù)命令分別處理數(shù)據(jù)*/2

12、8.if(currCMD.equals(1)29.out.write(OK”.getBytes();30.out.flush();31.elseif(currCMD.equals(2)32.out.write(OK.getBytes();33.out.flush();34.elseif(currCMD.equals(3)35.out.write(OK.getBytes();36.out.flush();37.elseif(currCMD.equals(4)38./*準(zhǔn)備接收文件數(shù)據(jù)*/39.try40.out.write(servicereceiveOK.getBytes();41.out.f

13、lush();42.catch(lOExceptione)43.e.printStackTrace();44.45.46./*接收文件數(shù)據(jù),4 字節(jié)文件長(zhǎng)度,4 字節(jié)文件格式,其后是文件數(shù)據(jù)*/filebytes=receiveFileFromSocket(in,out,filelength,fileformat);/Log.v(Service139.TAG,receivedata=+newtryFilefile=FileHelper.newFile(R0013340.JPG);filebytes.length);catch(IOExceptione)elseif(currCMD.equals

14、(exit)Log.e(androidService.TAG,Thread.currentThread().getName()+readwriteerror111111);Log.e(androidService.TAG,Thread.currentThread().getName()+readwriteerror222222);47.bytefilelength=newbyte4;48.bytefileformat=newbyte4;49.bytefilebytes=null;50.51./*從 socket 流中讀取完整文件數(shù)據(jù)*/63.e.printStackTrace();64.66.

15、67.68.catch(Exceptione)69./try70./out.write(error.getBytes(utf-8);71./out.flush();72./catch(IOExceptione1)73./e1.printStackTrace();74./84.e.printStackTrace();85.finally5.56./String(filebytes);57.58./*生成文件*/59.60.FileHelper.writeFile(file,filebytes,0,8.79.out.close();80.i

16、n.close();81.catch(Exceptione)82.83.87.if(client!=null)88.Log.v(androidService.TAG,Thread.currentThread().getName()89.+-+client.close();90.client.close();91.92.catch(IOExceptione)93.Log.e(androidService.TAG,Thread.currentThread().getName()94.+-+readwriteerror333333);95.e.printStackTrace();96.97.publ

17、icvoidrun()Log.d(androidService.TAG,Thread.cu+aclienthasconBufferedOutputStreamout;BufferedInputStreamin;try/*PC 端發(fā)來(lái)的數(shù)據(jù) msg*/StringcurrCMD=;out=newBufferedOutputStrin=newBufferedInputStrea/testSocket();/測(cè)試 sockandroidService.ioThreadFlagwhile(androidService.ioThrtryif(!client6.如果是在 PC 端和 android 端的讀

18、寫操作來(lái)因?yàn)?1”是只有在 socket 關(guān)閉時(shí)才作為判斷結(jié)尾。7.socket 在 out.write(bytes);時(shí),要是數(shù)據(jù)太大時(shí),超過(guò) socket 的緩存,socket 自動(dòng)分包發(fā)送,所以對(duì)方就一定要用循環(huán)來(lái)多次讀。最好的辦法就是服務(wù)器和客戶端協(xié)議好,比如發(fā)文件時(shí),先寫過(guò)來(lái)一個(gè)要發(fā)送的文件的大小,然后再發(fā)送文件;對(duì)方用這個(gè)大小,來(lái)循環(huán)讀取數(shù)據(jù)。android 端接收數(shù)據(jù)的代碼:viewplaincopytoclipboardprint?brewhile(true)循環(huán),這樣 socket 流的結(jié)尾不好判斷,不能用-1”來(lái)判斷,1./*2.*功能:從 socket流中讀取完整文件數(shù)據(jù)

19、3.4.*InputStreamin:socket 輸入流5.6.*bytefilelength:流的前 4 個(gè)字節(jié)存儲(chǔ)要轉(zhuǎn)送的文件的字節(jié)數(shù)7.8.*bytefileformat:流的前 5-8 字節(jié)存儲(chǔ)要轉(zhuǎn)送的文件的格式(如.apk)*功能:從 socket 流中讀取完整文件數(shù)據(jù)*InputStreamin:socket 輸入流*bytefilelength:流白前 4 個(gè)字節(jié)存儲(chǔ)要轉(zhuǎn)送*bytefileformat:流的前 5-8 字節(jié)存儲(chǔ)要轉(zhuǎn)送*/publicstaticbytereceiveFileFromSocket(OutputStreamout,bytefibytefilebyt

20、es=null;/文件數(shù)據(jù)tryintfilelen=MyUtil.bytesTStringstrtmp=readfileout.write(strtmp.getBytes(out.flush();/*4.35.*/publicstaticbytereceiveFileFromSocket(InputStreamin,OutputStreamout,bytefilelength,bytefileformat)bytefilebytes=null;/文

21、件數(shù)據(jù)tryintfilelen=MyUtil.bytesToInt(filelength);/Stringstrtmp=readfilelengthok:+filelen;out.write(strtmp.getBytes(utf-8);out.flush();filebytes=newbytefilelen;intpos=intrcvLen=36.文件長(zhǎng)度從 4 字節(jié) byte轉(zhuǎn)成 Int0;0;while(rcvLen=in.read(filebytes,pos,filelen-pos)pos+=rcvLen;Log.v(androidService.TAG,Thread.current

22、Thread().getName()+readfileOK:filesize=+filebytes.length);out.write(readfileok.getBytes(utf-8);out.flush();catch(Exceptione)Log.v(androidService.TAG,Thread.currentThread().getName()+receiveFileFromSocketerror);e.printStackTrace();returnfilebytes;0)8.socket 的最重要的機(jī)制就是讀寫采用的是阻塞的方式,如果客戶端作為命令發(fā)起者,服務(wù)器端作為接收者

23、的話,只有當(dāng)客戶端 client 用 out.writer()寫到輸出流里后,即流中有數(shù)據(jù) service 的 read 才會(huì)執(zhí)行,不然就會(huì)一直停在 read()那里等數(shù)據(jù)。9.還要讓服務(wù)器端可以同時(shí)連接多個(gè) client,即服務(wù)器端用 newthread()來(lái)作數(shù)據(jù)讀取操作。源碼:客戶端(pc 端):iCopyofJestPCClirrt,工srcJ.J(defaultparkage)jzileHelper.JavdtertPcClientjava%jtools.JavatestPcClient.javaviewplaincopytoclipboardprint?1.importjava.i

24、o.BufferedInputStream;2.importjava.io.BufferedOutputStream;3.importjava.io.BufferedReader;4.importjava.io.ByteArrayOutputStream;5.importjava.io.IOException;6.importjava.io.InputStream;7.importjava.io.InputStreamReader;.InetAddress;.Socket;10. .UnknownHostException;11.12. publicclasstestPcClient13.14

25、. /*15. *paramargs16. *throwsInterruptedException17. */18. publicstaticvoidmain(Stringargs)throwsInterruptedException19. try20. Runtime.getRuntime().exec(21. adbshellambroadcast-aNotifyServiceStop);22.Thread.sleep(3000);23.Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);24.Thread.sleep(3000)

26、;25.Runtime.getRuntime().exec(26.adbshellambroadcast-aNotifyServiceStart);27.Thread.sleep(3000);28.catch(IOExceptione3)29.e3.printStackTrace();30.31.32.Socketsocket=null;33.try34.InetAddressserverAddr=null;35.serverAddr=InetAddress.getByName();36.System.out.println(TCP1111+C:Connecting.);37

27、.socket=newSocket(serverAddr,12580);38.Stringstr=hi,wufenglong;39.System.out.println(TCP221122+C:RECEIVE);40.BufferedOutputStreamout=newBufferedOutputStream(socket41.getOutputStream();42.BufferedInputStreamin=newBufferedInputStream(socket43.getInputStream();44.BufferedReaderbr=newBufferedReader(newI

28、nputStreamReader(45.System.in);46.booleanflag=true;47.while(flag)48.System.out.print(請(qǐng)輸入 16 的數(shù)字,退出輸入 exit:);49.StringstrWord=br.readLine();/從控制臺(tái)輸入 1650.if(strWord.equals(T)51.out.write(1.getBytes();52.out.flush();53.System.out.println(1finishsendingthedata);54.StringstrFormsocket=readFromSocket(in);

29、55.System.out.println(thedatasentbyserveris:/r/n56.+strFormsocket);57.System.out58.println(=);59.elseif(strWord.equals(2)60.out.write(2.getBytes();System.out.println(2finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:/r/n+strFormsocket);System.out.

30、println(=);elseif(strWord.equals(3)out.write(3.getBytes();out.flush();System.out.println(3finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:/r/n+strFormsocket);System.out.println(=);elseif(strWord.equals(4)/*發(fā)送命令*/out.write(4.getBytes();out.flush()

31、;System.out.println(sendfilefinishsendingtheCMD:);/*服務(wù)器反饋:準(zhǔn)備接收*/StringstrFormsocket=readFromSocket(in);System.out.println(servicereadyreceicedata:UPDATE_CONTACTS:+strFormsocket);bytefilebytes=FileHelper.readFile(R0013340.JPG);System.out.println(filesize=+filebytes.length);/*將整數(shù)轉(zhuǎn)成 4 字節(jié) byte 數(shù)組*/bytef

32、ilelength=newbyte4;filelength=ToByte(filebytes.length);/*將.apk 字符串轉(zhuǎn)成 4 字節(jié) byte 數(shù)組*/bytefileformat=null;fileformat=.apk.getBytes();System.out.println(fileformatlength=+fileformat.length);/*字節(jié)流中前 4 字節(jié)為文件長(zhǎng)度,4 字節(jié)文件格式,以后是文件流*/*注意如果 write 里的 byte超過(guò) socket 的緩存,系統(tǒng)自動(dòng)分包寫過(guò)去,所以對(duì)方要循環(huán)寫*/out.write(filelen

33、gth);8.99.Stringstrok1=readFromSocket(in);System.out.println(servicereceivefilelength:+strok1);/out.write(fileformat);/out.flush();/Stringstrok2=readFromSocket(in);/System.out.println(service

34、receivefileformat:+/strok2);System.out.println(writedatatoandroid);out.write(filebytes);out.flush();System.out.println(*);/*服務(wù)器反饋:接收成功*/Stringstrread=readFromSocket(in);System.out.println(senddatasuccess:+strread);System.out.println(=);elseif(strWord.equalsIgnoreCase(EXIT)out.write(EXIT.getBytes();o

35、ut.flush();System.out.println(EXITfinishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:/r/n+strFormsocket);flag=false;System.out.println(=);catch(UnknownHostExceptione1)System.out.println(TCP331133+ERROR:+e1.toString();catch(Exceptione2)System.out.pri

36、ntln(TCP441144+ERROR:+e2.toString();finallytryif(socket!=null)socket.close();System.out.println(socket.close();141.catch(lOExceptione)0042.System

37、.out.println(TCP5555+ERROR:+e.toString();46.147./*從 InputStream 流中讀數(shù)據(jù)*/148.publicstaticStringreadFromSocket(InputStreamin)149.intMAX_BUFFER_BYTES=4000;150.Stringmsg=;151.bytetempbuffer=newbyteMAX_BUFFER_BYTES;152.numReadedBytes=in.read(tempbuffer,0,tempbuffer.length);154.msg=n

38、ewString(tempbuffer,0,numReadedBytes,utf-8);155.156.tempbuffer=null;157.catch(Exceptione)158.e.printStackTrace();159.160./Log.v(Service139.TAG,msg=+msg);161.returnmsg;162.163.importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.ByteArrayOutp

39、utStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;.InetAddress;.Socket;.UnknownHostException;publicclasstestPcClient/*paramargs*throwsInterruptedException*/publicstaticvoidmain(String口args)throandroid 服務(wù)器端:1產(chǎn)src3com,otheri,serviceL也androidSenriceJava|JiServ

40、iceBroadcastReceiwerjaval?出ThreadReadWriteriOSockdjava#田com.othen,util口出FilHelper.java中J|MyUtil.java主類 androidService.javaviewplaincopytoclipboardprint?1.packagecom.otheri.service;2.3.importjava.io.File;4.importjava.io.IOException;.ServerSocket;.Socket;7.8.importandroid.app.Service;9.importandroid.c

41、ontent.BroadcastReceiver;10.importandroid.content.Context;11.importandroid.content.Intent;12.importandroid.content.IntentFilter;13.importandroid.os.IBinder;14.importandroid.util.Log;15.16./*17.*設(shè)置:android 手機(jī)18.*19.*20.*/21.publicclassandroidServiceextendsService22.publicstaticfinalStringTAG=TAG;23.p

42、ublicstaticBooleanmainThreadFlag=true;24.publicstaticBooleanioThreadFlag=true;25.ServerSocketserverSocket=null;26.finalintSERVER_PORT=10086;27.FiletestFile;28.privatesysBroadcastReceiversysBR;29.30.Override31.publicvoidonCreate()32.super.onCreate();33.Log.v(TAG,Thread.currentThread().getName()+-+onC

43、reate);34./*創(chuàng)建內(nèi)部類 sysBroadcastReceiver 并注冊(cè) registerReceiver*/35.sysRegisterReceiver();36.newThread()37.publicvoidrun()38.doListen();39.;40.start();41.42.43.privatevoiddoListen()44.Log.d(TAG,Thread.currentThread().getName()+-45.+”doListen()START);46.serverSocket=null;47.try48.Log.d(TAG,Thread.current

44、Thread().getName()+-49.+doListen()newserverSocket);50.serverSocket=newServerSocket(SERVER_PORT);51.52.booleanmainThreadFlag=true;53.while(mainThreadFlag)54.Log.d(TAG,Thread.currentThread().getName()+-55.+doListen()listen);56.57.Socketclient=serverSocket.accept();58.59.newThread(newThreadReadWriterIO

45、Socket(this,client).start();60.61.catch(IOExceptione1)62.Log.v(androidService.TAG,Thread.currentThread().getName()63.+newserverSocketerror);64.e1.printStackTrace();65.67.68./*創(chuàng)建內(nèi)部類 sysBroadcastReceiver 并注冊(cè) registerReceiver*/69.privatevoidsysRegisterReceiver()70.Log.v(TAG,Thread.currentThread().getNa

46、me()+-71.+sysRegisterReceiver);72.sysBR=newsysBroadcastReceiver();73./*注冊(cè) BroadcastReceiver*/74.IntentFilterfilter1=newIntentFilter();75./*新的應(yīng)用程序被安裝到了設(shè)備上的廣播*/76.filter1.addAction(ent.action.PACKAGE_ADDED);77.filter1.addDataScheme(package);78.filter1.addAction(ent.action.PACKAGE

47、_REMOVED);79.filter1.addDataScheme(package);80.registerReceiver(sysBR,filterl);81.82.83./*內(nèi)部類:BroadcastReceiver 用于接收系統(tǒng)事件*/84.privateclasssysBroadcastReceiverextendsBroadcastReceiver85.86.Override87.publicvoidonReceive(Contextcontext,Intentintent)88.Stringaction=intent.getAction();89.if(action.equals

48、IgnoreCase(ent.action.PACKAGE_ADDED)90./ReadInstalledAPP();91.elseif(action92.equalsIgnoreCase(ent.action.PACKAGE_REMOVED)93./ReadInstalledAPP();94.95.Log.v(TAG,Thread.currentThread().getName()+-96.+sysBroadcastReceiveronReceive);00.Override101.publicvoidonDestroy()10

49、2.super.onDestroy();103.104./關(guān)閉線程105.mainThreadFlag=false;107./關(guān)閉服務(wù)器108.try109.Log.v(TAG,Thread.currentThread().getName()+-110.+serverSocket.close();111.serverSocket.close();112.catch(IOExceptione)113.e.printStackTrace();114.115.Log.v(TAG,Thread.currentThread().getName()+-117.118.119.Override120.pub

50、licvoidonStart(Intentintent,intstartId)121.Log.d(TAG,Thread.currentThread().getName()+-+122.super.onStart(intent,startId);26.Override127.publicIBinderonBind(Intentarg0)128.Log.d(TAG,onBind);129.returnnull;130.131.132.packagecom.otheri.service;importjava.io.File;importjava.io.IOException

51、;.ServerSocket;.Socket;importandroid.app.Service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.os.IBinder;importandroid.util.Log;/*設(shè)置:android 手機(jī)*116.u*onDestroy*”);onStart();用于接收 PC 發(fā)來(lái)的 Broastcast 并

52、啟動(dòng)主類 service 的ServiceBroadcastReceiver.javaviewplaincopytoclipboardprint?1.packagecom.otheri.service;2.3.importandroid.content.BroadcastReceiver;4.importandroid.content.Context;5.importandroid.content.Intent;6.importandroid.util.Log;7.8.publicclassServiceBroadcastReceiverextendsBroadcastReceiver9.pr

53、ivatestaticStringSTART_ACTION=NotifyServiceStart;10.privatestaticStringSTOP_ACTION=NotifyServiceStop;11.12.Override13.publicvoidonReceive(Contextcontext,Intentintent)14.Log.d(androidService.TAG,Thread.currentThread().getName()+-15.+ServiceBroadcastReceiveronReceive);16.17.Stringaction=intent.getActi

54、on();18.if(START_ACTION.equalsIgnoreCase(action)19.context.startService(newIntent(context,androidService.class);20.21.Log.d(androidService.TAG,Thread.currentThread().getName()+-22.+ServiceBroadcastReceiveronReceivestartend);23.elseif(STOP_ACTION.equalsIgnoreCase(action)24.context.stopService(newInte

55、nt(context,androidService.class);25.Log.d(androidService.TAG,Thread.currentThread().getName()+-26.+ServiceBroadcastReceiveronReceivestopend);0.packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log

56、;publicclassServiceBroadcastReceiverextendsBroadprivatestaticStringSTART_ACTION=NotifprivatestaticStringSTOP_ACTION=NotifyOverridepublicvoidonReceive(Contextcontext,InteLog.d(androidService.TAG,Thread.cu+ServiceBroadcastStringaction=intent.getAction();if(START_ACTION.equalsIgnoreCase(a用于新 socket 連接的

57、讀寫線程類 ThreadReadWriterIOSocket.javaviewplaincopytoclipboardprint?1.packagecom.otheri.service;2.3.importjava.io.BufferedInputStream;4.importjava.io.BufferedOutputStream;5.importjava.io.ByteArrayOutputStream;6.importjava.io.File;7.importjava.io.IOException;8.importjava.io.InputStream;9.importjava.io.O

58、utputStream;.Socket;11.12.importandroid.content.Context;13.importandroid.util.Log;14.15.importcom.otheri.util.FileHelper;16.importcom.otheri.util.MyUtil;17.18./*19.*功能:用于 socket 的交互20.*21.*authorwufenglong22.*23.*/24.publicclassThreadReadWriterIOSocketimplementsRunnable25.privateSocketclient;26.priv

59、ateContextcontext;27.28.ThreadReadWriterIOSocket(Contextcontext,Socketclient)29.30.this.client=client;31.this.context=context;32.33.34.Override35.publicvoidrun()36.Log.d(androidService.TAG,Thread.currentThread().getName()+-37.+aclienthasconnectedtoserver!);38.BufferedOutputStreamout;39.BufferedInput

60、Streamin;40.try41./*PC 端發(fā)來(lái)的數(shù)據(jù) msg*/42.StringcurrCMD=;43.out=newBufferedOutputStream(client.getOutputStream();44.in=newBufferedInputStream(client.getInputStream();45./testSocket();/測(cè)試 socket 方法46.androidService.ioThreadFlag=true;47.while(androidService.ioThreadFlag)48.try49.if(!client.isConnected()50

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論