android手机端与PC端使用adb forword通信
PC端与Android手机端使用adbforword通信
服务器端代码如下:
importjava.io.IOException; importjava.io.ObjectOutputStream; importjava.net.Socket; importjava.net.UnknownHostException; importjava.util.Scanner; publicclassServer{ publicstaticfinalStringTAG="server"; publicstaticintPC_LOCAL_PORT=22222; publicstaticintPHONE_PORT=22222; publicstaticStringADB_PATH="adb.exe"; /** *@paramargs */ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub YingyonghuiHubServer.execAdb(); } publicstaticvoidexecAdb(){ //runtheadbbridge try{ Processp=Runtime.getRuntime().exec( ADB_PATH+"forwardtcp:"+PC_LOCAL_PORT+"tcp:" +String.valueOf(PHONE_PORT)); Scannersc=newScanner(p.getErrorStream()); //Ifthereissomeoutput,itfailedtostartadb if(sc.hasNext()){ while(sc.hasNext()) System.out.println(sc.next()); System.err.println("CannotstarttheAndroiddebugbridge"); return; } initializeConnection(); }catch(Exceptione){ System.err.println(e.toString()); } } staticSocketsocket; publicstaticvoidinitializeConnection(){ //Createsocketconnection try{ socket=newSocket("localhost",PC_LOCAL_PORT); ObjectOutputStreamoos=newObjectOutputStream( socket.getOutputStream()); oos.writeObject("lalala"); oos.close(); socket.close(); }catch(UnknownHostExceptione){ System.err.println("Socketconnectionproblem(Unknownhost)" +e.getStackTrace()); e.printStackTrace(); }catch(IOExceptione){ System.err.println("CouldnotinitializeI/Oonsocket"); e.printStackTrace(); } } }
客户端代码如下:
importjava.io.IOException; importjava.io.ObjectInputStream; importjava.net.ServerSocket; importjava.net.Socket; importandroid.app.Activity; importandroid.content.Context; importandroid.os.AsyncTask; importandroid.os.Bundle; importandroid.util.Log; importandroid.widget.TextView; importandroid.widget.Toast; publicclassClientextendsActivity{ publicstaticfinalStringTAG="client"; publicstaticintPHONE_PORT=22222; ContextmContext=null; TextViewtextView=null; ServerSocketserver=null; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); this.mContext=this; this.textView=(TextView)this.findViewById(R.id.textView1); try{ server=newServerSocket(PHONE_PORT); }catch(IOExceptione){ e.printStackTrace(); return; } newRepackTestTask().execute(); } privateclassRepackTestTaskextendsAsyncTask{ @Override protectedObjectdoInBackground(Object...params){ Socketclient=null; //initializeserversocket while(true){ try{ //attempttoacceptaconnection client=server.accept(); Log.d(TAG,"Getaconnectionfrom" +client.getRemoteSocketAddress().toString()); ObjectInputStreamois=newObjectInputStream( client.getInputStream()); Stringsomewords=(String)ois.readObject(); Log.d(TAG,"Getsomewords"+somewords); this.publishProgress(somewords); client.close(); }catch(IOExceptione){ Log.e(TAG,""+e); }catch(ClassNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } @Override protectedvoidonProgressUpdate(Object...values){ super.onProgressUpdate(values); Toast.makeText(mContext,values[0].toString(),Toast.LENGTH_LONG) .show(); } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!