3

通过查看 Microsoft 网站上的示例应用程序How to connect with a stream socket (XAML) ,我了解了如何连接到服务器,并将字符串发送到服务器。但是,该示例并没有完全扩展到从套接字读取数据。

服务器是 ac# windows 控制台应用程序,它的作用是使用网络流向移动客户端发送数据。

        //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");

为了从流中读取数据,DataReader使用了该类。我对 c# Mobile 很陌生,DataReader 类的文档没有提供任何示例实现,那么如何从 Stream Socket 读取数据?

使用微软的示例代码;

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

但现在我不确定如何读取服务器发送的字节数组。

4

0 回答 0