从查看Microsoft网站上的示例应用程序How to connect with a stream socket (XAML) ,我已经学会了如何连接到服务器,并将字符串发送到服务器。但是,该示例并未完全扩展从套接字读取数据。
服务器是一个c#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中读取数据?
使用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);
但是现在我不确定如何读取从服务器发送的字节数组。