user6845064
user6845064

Reputation:

Windows IoT TcpClient

I want to send a command to my Sonos speaker. With my Windows application that is easy. I just use the TcpClient example provided on Microsoft website (shown below).

    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();
    }

Now, how would I go about doing this with Windows 10 IoT on a Raspberry Pi 3?

Upvotes: 0

Views: 1306

Answers (1)

Jackie
Jackie

Reputation: 2030

With UWP, you may need to reference the "System.Net.Sockets" Nuget package in order to use TcpClient. You probably end up with something like below snippet,

    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();
    }

Note that you need to declare the Internet client capability in your project manifest file.

enter image description here PS: There's also an alternative an alternative called StreamSocket.

Refer to an complete code sample from Microsoft github repository.

Also, if you're new to UWP programming, you should get yourself familar with the async/await pattern.

Upvotes: 0

Related Questions