2014-11-03 172 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