Reading data from a StreamSocket

1k Views Asked by At

From looking at the example application on the Microsoft Website How to connect with a stream socket (XAML) , I have learned how to connect to a server, and send string to the server. However, the example doesn't quite extend on reading data from the socket.

The server is a c# windows console application and what it does is send data to the mobile client by using a network stream.

        //send user response
        //message is a frame packet containing information
        message = new Message();
        //type 1 just means it's successfull
        message.type = 1;
        //using Newton JSON i convert the Message Object into a string object
        string sendData = JsonConvert.SerializeObject(message);
        //conver the string into a bytearray and store in the variable data (type byte array)
        byte[] data = GetBytes(sendData);
        //netStream is my NetworkStream i want to write the byte array called data, starting to from 0, and ending at the last point in array
        netStream.Write(data, 0, data.Length);
        //flushing the stream, not sure why, flushing means to push data
        netStream.Flush();
        //debugging
        Console.WriteLine("sent");

In order to read data from a stream, the DataReader class is used. I am quite new to c# Mobile, and the documentation on the DataReader class doesn't provide any implementations of examples, so how can I read data from the Stream Socket?

Using the example code from microsoft;

            DataReader reader = new DataReader(clientSocket.InputStream);
            // Set inputstream options so that we don't have to know the data size
            reader.InputStreamOptions = InputStreamOptions.Partial;
            await reader.LoadAsync(reader.UnconsumedBufferLength);

But now I am not sure how to read the byte array sent from the server.

0

There are 0 best solutions below