时间:2021-05-20
PC端与Android手机端使用adb forword通信
服务器端代码如下:
import java.io.IOException; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Server { public static final String TAG = "server"; public static int PC_LOCAL_PORT = 22222; public static int PHONE_PORT = 22222; public static String ADB_PATH = "adb.exe"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub YingyonghuiHubServer.execAdb(); } public static void execAdb() { // run the adb bridge try { Process p = Runtime.getRuntime().exec( ADB_PATH + " forward tcp:" + PC_LOCAL_PORT + " tcp:" + String.valueOf(PHONE_PORT)); Scanner sc = new Scanner(p.getErrorStream()); // If there is some output, it failed to start adb if (sc.hasNext()) { while (sc.hasNext()) System.out.println(sc.next()); System.err.println("Cannot start the Android debug bridge"); return; } initializeConnection(); } catch (Exception e) { System.err.println(e.toString()); } } static Socket socket; public static void initializeConnection() { // Create socket connection try { socket = new Socket("localhost", PC_LOCAL_PORT); ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream()); oos.writeObject("lalala"); oos.close(); socket.close(); } catch (UnknownHostException e) { System.err.println("Socket connection problem (Unknown host)" + e.getStackTrace()); e.printStackTrace(); } catch (IOException e) { System.err.println("Could not initialize I/O on socket"); e.printStackTrace(); } } }客户端代码如下:
import java.io.IOException; import java.io.ObjectInputStream; import java.net.ServerSocket; import java.net.Socket; import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class Client extends Activity { public static final String TAG = "client"; public static int PHONE_PORT = 22222; Context mContext = null; TextView textView = null; ServerSocket server = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.mContext = this; this.textView = (TextView) this.findViewById(R.id.textView1); try { server = new ServerSocket(PHONE_PORT); } catch (IOException e) { e.printStackTrace(); return; } new RepackTestTask().execute(); } private class RepackTestTask extends AsyncTask { @Override protected Object doInBackground(Object... params) { Socket client = null; // initialize server socket while (true) { try { // attempt to accept a connection client = server.accept(); Log.d(TAG, "Get a connection from " + client.getRemoteSocketAddress().toString()); ObjectInputStream ois = new ObjectInputStream( client.getInputStream()); String somewords = (String) ois.readObject(); Log.d(TAG, "Get some words" + somewords); this.publishProgress(somewords); client.close(); } catch (IOException e) { Log.e(TAG, "" + e); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG) .show(); } } }感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android客户端请求服务器端的详细解释1.Android客户端与服务器端通信方式:Android与服务器通信通常采用HTTP通信方式和Socket通信方式,
主要功能:用于在PC端通过ADB工具抓取指定android真机上运行的指定包名应用的调试日志,并生成本地txt文件。实现步骤:1、准备ADB调试软件;2、根据包
qq客户端是指安装在手机里面的腾讯QQ软件,就像在PC端(电脑端)上下载的腾讯QQ一样,是为了方便手机用户使用移动QQ通信。 QQ(TencentQQ)是腾讯
现在淘宝店铺手机端已经远远超过PC端了,但是并不排除还有用户使用PC端。我们作为淘宝卖家,不仅仅要做好PC端的设计,手机端的设计也要重视起来了。那么,下面我们就
PC端与手机端相比,虽然流量比不过,但是PC端以大取胜,单页浏览的商品远远大于手机端。要想提升手机端的转化率,我们必须在呈现内容的时候更加直接有效。那么如何