2014-11-03 182 views
1

我试图从我的Windows手机发送信息到计算机。我读了一些usb电缆被视为以太网电缆的地方。我创建了一个服务器和一个客户端(电话是客户端)来尝试发送信息。程序每次按下输入时都会发送一条消息。从Windows Phone 8发送信息到PC窗口7窗体

客户端

public sealed partial class MainPage : Page 
{ 

    private bool Connected; 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     this.NavigationCacheMode = NavigationCacheMode.Required; 
    } 

    /// <summary> 
    /// Invoked when this page is about to be displayed in a Frame. 
    /// </summary> 
    /// <param name="e">Event data that describes how this page was reached. 
    /// This parameter is typically used to configure the page.</param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     // TODO: Prepare page for display here. 

     // TODO: If your application contains multiple pages, ensure that you are 
     // handling the hardware Back button by registering for the 
     // Windows.Phone.UI.Input.HardwareButtons.BackPressed event. 
     // If you are using the NavigationHelper provided by some templates, 
     // this event is handled for you. 
    } 

    private DatagramSocket dataGramSocket = new DatagramSocket(); 
    private DataWriter socketWriter; 
    private bool messageSent; 
    private static string port = "138"; 

    private void Grid_Loaded(object sender, RoutedEventArgs e) 
    { 
     dataGramSocket.MessageReceived += dataGramSocket_MessageReceived; 
     StartListener(); 
    } 

    void dataGramSocket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args) 
    { 

    } 

    private async void StartListener() 
    { 


     string IpHostname = "127.0.0.1"; 

     var IpAdresses = NetworkInformation.GetHostNames(); 


     for (int i = 0; i < IpAdresses.Count; i++) 
     { 
      if (IpAdresses[i].IPInformation != null) 
      { 
       if (IpAdresses[i].IPInformation.NetworkAdapter.IanaInterfaceType == 6 && IpAdresses[i].DisplayName != null) 
       { 
        IpHostname = IpAdresses[i].DisplayName; 
        break; 
       } 
       else if (IpAdresses[i].IPInformation.NetworkAdapter.IanaInterfaceType == 71 && IpAdresses[i].DisplayName != null) 
       { 
        IpHostname = IpAdresses[i].DisplayName; 
        break; 
       } 

      } 

     } 





     HostName host = new HostName(IpHostname); 
     //EndpointPair endpoint = new EndpointPair(localHostName,) 
     await dataGramSocket.BindServiceNameAsync(port); 
     await dataGramSocket.ConnectAsync(host, port); 
     socketWriter = new DataWriter(dataGramSocket.OutputStream); 
     Connected = true; 
    } 


    private async void SendPacket() 
    { 
     await socketWriter.StoreAsync(); 
     messageSent = true; 
    } 

    private void TextBox1_KeyDown(object sender, KeyRoutedEventArgs e) 
    { 
     if(e.Key == Windows.System.VirtualKey.Enter) 
     { 
      SendMessage(textBox1.Text); 
     } 
    } 

    private void SendMessage(string message) 
    { 
     socketWriter.WriteString(message); 
     SendPacket(); 
     textBox1.Text = ""; 
    } 

} 

}

服务器端

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private static int port = 138; 
    private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); 
    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 
     Task.Factory.StartNew(() => 
     { 
      var dataStream = new MemoryStream(1024); 
      var udpClient = new UdpClient(port); 
      while (true) 
      { 
       if (udpClient.Available > 0) 
       { 
        udpClient.BeginReceive(ar => 
        { 
         var clientEndPoint = new IPEndPoint(IPAddress.Any, port); 
         var bytesReceived = udpClient.EndReceive(ar, ref clientEndPoint); 
         dataStream.Write(bytesReceived, 0, bytesReceived.Length); 
         if (bytesReceived.Length > 0) 
         { 
          UpdateUI(Encoding.UTF8.GetString(bytesReceived)); 
          UpdateUI(Encoding.ASCII.GetString(bytesReceived)); 

         } 
        }, null); 
       } 
       Thread.Sleep(1); 
      } 
     }, _cancellationTokenSource.Token); 
    } 

    private void UpdateUI(string message) 
    { 
     this.Invoke(new MethodInvoker(delegate 
     { 
      this.textBox1.Text += message + Environment.NewLine; 
     })); 
    } 

    private void button1_Click_1(object sender, EventArgs e) 
    { 
     byte[] message = Encoding.UTF8.GetBytes(textBox2.Text); 
     using (var udpClient = new UdpClient()) 
     { 
      udpClient.Send(message, message.Length, new IPEndPoint(IPAddress.Loopback, port)); 
     } 
     textBox2.Clear(); 
    } 
} 

}

的信息并不在中间的某个地方连接。双方没有任何突破。当我通过向loopback发送信息来测试表单时,它工作正常。我不知道为什么信息没有到达另一边。你能否告诉我,如果我正在写这个文件,而且这样做是假设这样做,或者你能告诉我这是不可能的,所以我可以停止对它进行处理。

回答

0

我看到一个类似的问题,我在那里回答,Communicate to PC over USB

它需要编辑注册表,设备上运行的服务器代码,我不确定代码是否会在开发环境之外运行(我只需要这样来加速我的工作流程和调试 - 所以它不是问题)。

+0

当我尝试了它说我需要将手机转换成大容量存储设备。我有诺基亚Lumia 920和521。我没有看到关于这个在线的教程。我发现注册表键的那个不是extis。你知道我可以如何将手机转换成这个吗? – 2014-11-13 06:42:20

+0

您可以忽略“设置电话”部分,即针对启用了内核调试的OEM。我使用SDK for Windows Phone 8.1,注册表键已经存在,我可以通过运行“IpOverUsbEnum.exe”来验证它们,我不必在设备上执行任何操作。 – MichalisB 2014-11-19 12:28:00

0

不可能。

通过USB的TCP/IP在Windows Phone 7中工作正常。但是在Windows Phone 8中,他们删除了这些功能。

寻求其他的选择。

您可以通过WiFi使用TCP/IP或使用BT或将数据写入“文档”并使用MTP COM API读取数据,或将数据写入独立存储并使用isetool.exe进行读取(此功能仅适用于dev.unlocked设备)。

+0

谢谢,我想知道为什么udp无法通过wifi工作?在我的部分代码中,我获得了目前我所连接的无线网络并发送信息。它仍然无法工作。需要使用udp而不是tcp,因为我需要像流式音频和发送文本一样快速发送信息。 – 2014-11-04 02:48:33

+0

A1。因为你将它们发送到127.0.0.1而不是你的PC的地址? A2。因为您的Windows防火墙会阻止该流量。 另外,如果我是你,我会选择1024以上的端口# - 1024以下的端口通常用于众所周知的服务。 – Soonts 2014-11-04 03:18:08

+0

使用ianainterfacetype的代码部分,如果连接到一个无线网络,我会得到IP的IP。我也只能切换到1024以下的端口,因为我尝试了9种不同的类型。有人建议我尝试一个“安全端口”。 – 2014-11-04 16:22:32