【问题标题】:Connecting to Android Emulator using .NET C#使用 .NET C# 连接到 Android 模拟器
【发布时间】:2013-04-29 22:01:23
【问题描述】:

我正在尝试使用 TcpClient 连接到 Android 模拟器。模拟器是在 localhost:5554 上运行的 Android 4.2.2,我从 AVD Manager 开始。我能够连接并发送“电源状态放电”命令,但在发送第二个命令后,程序挂起等待响应。当我使用 Putty 原始连接进行连接时,这些命令有效。

这里是完整的代码:

using System;
using System.Net.Sockets;
using System.Text;

namespace AndroidBatteryChangeEmulator
{
    class Program
    {
        private static readonly TcpClient connection = new TcpClient();

        static void Main(string[] args)
        {
            try
            {
                connection.Connect("localhost", 5554);
                NetworkStream stream = connection.GetStream();

                ReadDataToConsole(stream);
                SendCommand(stream, "power status discharging");

                string command = string.Format("power capacity {0}", 50);
                SendCommand(stream, command);

                stream.Close();
                connection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("The following error has occured: {0}", ex.Message);
            }
        }

        private static void ReadDataToConsole(NetworkStream stream)
        {
            var responseBytes = new byte[connection.ReceiveBufferSize];
            stream.Read(responseBytes, 0, connection.ReceiveBufferSize);

            string responseText = Encoding.ASCII.GetString(responseBytes).Trim(new[] { ' ', '\0', '\n', '\r' });
            if (!string.IsNullOrEmpty(responseText))
                Console.WriteLine("Response: '{0}'.", responseText);
        }

        private static void SendCommand(NetworkStream stream, string command)
        {
            Console.WriteLine("Sending command '{0}'.", command);
            byte[] commandBytes = Encoding.ASCII.GetBytes(command + "\r");
            Buffer.BlockCopy(command.ToCharArray(), 0, commandBytes, 0, commandBytes.Length);

            stream.Write(commandBytes, 0, commandBytes.Length);

            ReadDataToConsole(stream);
        }

    }
}

这是程序的输出:

Response: 'Android Console: type 'help' for a list of commands'.
Sending command 'power status discharging'.
Response: 'OK'.
Sending command 'power capacity 50'.

我不确定是什么导致了问题。

提前感谢您的帮助!

【问题讨论】:

    标签: android .net android-emulator avd networkstream


    【解决方案1】:

    如果有人想知道,我通过将NetworkStreamStreamReader 包装在ReadDataToConsole() 函数中和StreamWriterSendCommand() 函数中来解决了这个问题。 确保AutoFlushStreamWriter 中是true

    现在一切正常!

    【讨论】:

      【解决方案2】:

      您介意发布您的工作代码吗?这是我使用的代码(供未来的访问者使用):

      using (TcpClient client = new TcpClient(host, port))
      {
          using (NetworkStream stream = client.GetStream())
          {
              using (StreamReader reader = new StreamReader(stream))
              {
                  using (StreamWriter writer = new StreamWriter(stream))
                  {
                      writer.AutoFlush = true;
                      foreach (string command in commandList)
                      {
                          writer.WriteLine(command);
                          string response = reader.ReadLine();
                          Thread.Sleep(5000);
                      }
                  }
              }
          }
      }
      

      马丁

      【讨论】:

        猜你喜欢
        • 2011-10-11
        • 1970-01-01
        • 2011-10-17
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-14
        相关资源
        最近更新 更多