使用USB电缆将套接字消息发送到Android

时间:2018-07-05 14:50:26

标签: c# android adb

我具有一项功能,如果我将电话的IP(本地网络)放入电话中,则电话会收到消息,而不会出现任何问题,但是当我选择通过电缆发送消息时,消息会到达电话为空:/

这是我正在使用的功能:

private void buttonSendMessage_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPAddress ipAdd = IPAddress.Parse("192.168.1.9");
        String ipDest = "";

        String removeForward = "adb forward --remove-all";
        ProcessStartInfo ProcessInfo;
        Process proc = new Process();
        ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + removeForward);
        ProcessInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
        ProcessInfo.CreateNoWindow = true;
        proc.StartInfo = ProcessInfo;
        proc.Start();
        if (this.checkBoxCable.IsChecked == true)
        {                   
            String Command = "adb forward tcp:" + this.textBoxPort.Text + " tcp:" + this.textBoxPort.Text;
            ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
            ProcessInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            ProcessInfo.CreateNoWindow = false;
            proc.StartInfo = ProcessInfo;
            proc.Start();
            ipDest = "127.0.0.1";
        }
        else
        {
            ipDest = this.textBoxServer.Text;
        }

        socket.Connect(new IPEndPoint(IPAddress.Parse(ipDest), int.Parse(this.textBoxPort.Text)));

        byte[] byData = Encoding.ASCII.GetBytes(this.textBoxMessage.Text);
        socket.Send(byData);


        socket.Disconnect(false);
        socket.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

根据我在这里和互联网上阅读的其他一些问题,我们需要告诉adb转发,我这样做了,但仍然不起作用,如果我输入adb forwad --list,它表明正在转接到电话

编辑解决方案:

我确实找到了解决方案……只是添加了Thread.Sleep(250);,现在它确实可以毫无问题地发送数据了,无论是通过本地网络还是通过USB电缆都没有问题

这里是解决方案,以防万一

private void buttonSendMessage_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        String removeForward = "adb forward --remove-all";
        ProcessStartInfo ProcessInfo;
        Process proc = new Process();
        ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + removeForward);
        ProcessInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
        ProcessInfo.CreateNoWindow = true;
        proc.StartInfo = ProcessInfo;
        proc.Start();
        if (this.checkBoxCable.IsChecked == true)
        {                   
            String Command = "adb forward tcp:" + this.textBoxPort.Text + " tcp:" + this.textBoxPort.Text;
            ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
            ProcessInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            ProcessInfo.CreateNoWindow = false;
            proc.StartInfo = ProcessInfo;
            proc.Start();
            socket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(this.textBoxPort.Text)));
            System.Threading.Thread.Sleep(250);
        }
        else
        {
            socket.Connect(new IPEndPoint(IPAddress.Parse(this.textBoxServer.Text), int.Parse(this.textBoxPort.Text)));
        }
        byte[] byData = Encoding.ASCII.GetBytes(this.textBoxMessage.Text);
        socket.Send(byData);

        socket.Disconnect(false);
        socket.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

0 个答案:

没有答案