C# PC客户端与Android服务端的Socket同步通信(USB)

需求:

Android的apk获取手机信息,把结果发给PC client

注意事项:

1.android默认手机端的IP为“127.0.0.1”

2.要想联通PC与android手机的sokcet,一定要用adb forward 来作下端口转发才能连上socket.

3.使用socket通信,需要在mainfest.xml中添加permission: android.permission.INTERNET

  1. Runtime.getRuntime().exec("adbforwardtcp:12580tcp:10086");
  2. Thread.sleep(3000);

Android作为服务端:

  1. importjava.io.BufferedInputStream;
  2. importjava.io.BufferedOutputStream;
  3. importjava.io.IOException;
  4. importjava.net.InetAddress;
  5. importjava.net.ServerSocket;
  6. importjava.net.Socket;
  7. publicclassTcpConnectimplementsRunnable{
  8. privatefinalintSERVER_PORT=10086;
  9. privateServerSocketmServerSocket;
  10. privateSocketmClient;
  11. privateStringmDeviceId;
  12. privateStringmDeviceType;
  13. publicTcpConnect(StringaDeviceId,StringaDeviceType){
  14. this.mDeviceId=aDeviceId;
  15. this.mDeviceType=aDeviceType;
  16. try{
  17. Stringip=InetAddress.getLocalHost().getHostAddress();
  18. System.out.println("ip地址是:"+ip);
  19. //System.out.println(aDeviceId+"型号:"+aDeviceType);
  20. mServerSocket=newServerSocket(SERVER_PORT);
  21. System.out.println("TcpConnect"+"建立Socket");
  22. //listen();
  23. }catch(IOExceptione){
  24. //TODOAuto-generatedcatchblock
  25. //e.printStackTrace();
  26. System.out.println("TcpConnect"+e.getMessage());
  27. }
  28. }
  29. publicvoidlisten(){
  30. while(true){
  31. try{
  32. mClient=mServerSocket.accept();
  33. //Log.e("TcpConnect","在积极的监听");
  34. }catch(IOExceptione){
  35. //TODOAuto-generatedcatchblock
  36. //e1.printStackTrace();
  37. System.out.println("TcpConnect"+e.getMessage());
  38. }
  39. }
  40. }
  41. @Override
  42. publicvoidrun(){
  43. //TODOAuto-generatedmethodstub
  44. //if(mClient.isConnected()){
  45. BufferedOutputStreamout=null;
  46. System.out.println("TcpConnect"+"开始监听");
  47. while(true){
  48. try{
  49. //Log.e("TcpConnect","开始监听");
  50. mClient=mServerSocket.accept();
  51. //if(mClient.isConnected()){
  52. System.out.println("TcpConnect"+"检测到有连接");
  53. out=newBufferedOutputStream(mClient.getOutputStream());
  54. StringrecordStr=mDeviceId+"|"+mDeviceType;
  55. out.write(recordStr.getBytes("utf-8"));
  56. //intlength=recordStr.getBytes().length;
  57. //byte[]b=recordStr.getBytes();
  58. //out.writeInt(length);
  59. //out.write(b);
  60. out.flush();
  61. //Log.e("TcpConnect",recordStr);
  62. //out.flush();
  63. //}
  64. }catch(Exceptione){
  65. System.out.println("TcpConnect"+e.getMessage());
  66. }finally{
  67. if(out!=null){
  68. try{
  69. out.close();
  70. }catch(IOExceptione){
  71. //TODOAuto-generatedcatchblock
  72. System.out.println("TcpConnect"+e.getMessage());
  73. }
  74. }
  75. if(mServerSocket!=null){
  76. try{
  77. mServerSocket.close();
  78. }catch(IOExceptione){
  79. //TODOAuto-generatedcatchblock
  80. System.out.println("TcpConnect"+e.getMessage());
  81. }
  82. }
  83. //}
  84. }
  85. }
  86. }
  87. publicstaticvoidmain(String[]args){
  88. newThread(newTcpConnect("2366578546946","T959")).start();
  89. }
  90. }

C#作为客户端,在客户端进行绑定端口

  1. Processp=newProcess();//实例一个Process类,启动一个独立进程
  2. p.StartInfo.FileName="cmd.exe";//设定程序名
  3. p.StartInfo.UseShellExecute=false;//关闭Shell的使用
  4. p.StartInfo.RedirectStandardInput=true;//重定向标准输入
  5. p.StartInfo.RedirectStandardOutput=true;//重定向标准输出
  6. p.StartInfo.RedirectStandardError=true;//重定向错误输出
  7. p.StartInfo.CreateNoWindow=true;//设置不显示窗口
  8. p.StartInfo.ErrorDialog=false;
  9. p.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
  10. p.Start();
  11. p.StandardInput.WriteLine(@"adbforwardtcp:12580tcp:10086");
  12. //Thread.Sleep(3000);
  13. SocketClientclient=newSocketClient();
  14. MessageBox.Show("收到的数据为:"+client.listen());

C#的Socket客户端view plain

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Net;
  6. usingSystem.Net.Sockets;
  7. namespacePreInstaller.IO
  8. {
  9. classSocketClient
  10. {
  11. publicstringlisten()
  12. {
  13. Socketclient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  14. IPAddressmyIP=IPAddress.Parse("127.0.0.1");
  15. IPEndPointEPhost=newIPEndPoint(myIP,int.Parse("12580"));
  16. client.Connect(EPhost);
  17. byte[]t_data=newbyte[1024];
  18. stringdata=null;
  19. inti=0;
  20. while((i=client.Receive(t_data))!=0)
  21. {
  22. data=Encoding.UTF8.GetString(t_data,0,i);
  23. }
  24. client.Close();
  25. returndata;
  26. }
  27. }
  28. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值