Microsoft の Web サイトにあるサンプル アプリケーションの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);
しかし、サーバーから送信されたバイト配列を読み取る方法がわかりません。