2016-09-18 66 views
0

我想發送一個命令給我的Sonos揚聲器。用我的Windows應用程序很容易。我只使用Microsoft網站上提供的TcpClient示例(如下所示)。Windows IoT TcpClient

public void Connect(String server, String message) 
    { 
     try 
     {     
      TcpClient client = new TcpClient(server, 1400); 

      // Translate the passed message into ASCII and store it as a Byte array. 
      Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); 

      // Get a client stream for reading and writing. 
      // Stream stream = client.GetStream(); 

      NetworkStream stream = client.GetStream(); 

      // Send the message to the connected TcpServer. 
      stream.Write(data, 0, data.Length); 

      //Console.WriteLine("Sent: {0}", message); 

      // Receive the TcpServer.response. 

      // Buffer to store the response bytes. 
      data = new Byte[256]; 

      // String to store the response ASCII representation. 
      String responseData = String.Empty; 

      // Read the first batch of the TcpServer response bytes. 
      Int32 bytes = stream.Read(data, 0, data.Length); 
      responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); 
      //Console.WriteLine("Received: {0}", responseData); 

      // Close everything. 
      stream.Close(); 
      client.Close(); 
     } 
     catch (ArgumentNullException e) 
     { 
      //Console.WriteLine("ArgumentNullException: {0}", e); 
     } 
     catch (SocketException e) 
     { 
      // Console.WriteLine("SocketException: {0}", e); 
     } 

     //Console.WriteLine("\n Press Enter to continue..."); 
     //Console.Read(); 
    } 

現在,我將如何去做與Raspberry Pi 3上的Windows 10物聯網?

回答

0

使用UWP,您可能需要引用「System.Net.Sockets」Nuget包才能使用TcpClient。你可能最終得到類似下面片斷,

async void FunctionName() 
    { 
     try 
     { 
      using (var client = new TcpClient()) 
      { 
       await client.ConnectAsync(server, 1400); 

       // Translate the passed message into ASCII and store it as a Byte array. 
       Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); 

       // Get a client stream for reading and writing. 
       // Stream stream = client.GetStream(); 
       NetworkStream stream = client.GetStream(); 

       // Send the message to the connected TcpServer. 
       stream.Write(data, 0, data.Length); 

       //Console.WriteLine("Sent: {0}", message); 

       // Receive the TcpServer.response. 

       // Buffer to store the response bytes. 
       data = new Byte[256]; 

       // String to store the response ASCII representation. 
       String responseData = String.Empty; 

       // Read the first batch of the TcpServer response bytes. 
       Int32 bytes = stream.Read(data, 0, data.Length); 
       responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); 
       //Console.WriteLine("Received: {0}", responseData); 
      } 
     } 
     catch (ArgumentNullException e) 
     { 
      //Console.WriteLine("ArgumentNullException: {0}", e); 
     } 
     catch (SocketException e) 
     { 
      // Console.WriteLine("SocketException: {0}", e); 
     } 

     //Console.WriteLine("\n Press Enter to continue..."); 
     //Console.Read(); 
    } 

請注意,您需要在申報項目清單文件中的互聯網客戶端的能力。

enter image description here PS:還有一種替代方案叫做StreamSocket

請參閱Microsoft的github repository的完整代碼示例。另外,如果你是UWP編程的新手,你應該讓自己熟悉異步/等待模式。