Windows で TCP ソケットを使用したサーバーとクライアントの通信を作成し、正常に動作しましたが、クライアント側を Windows Phone に移植しようとしていますが、データの受信で本当に行き詰まっています。私は StreamSocket を使用しているため、データの長さを知る必要があります。例えば:
DataReader dataReader = new DataReader(clientSocket.InputStream);
uint bytesRead = 0;
bytesRead = await dataReader.LoadAsync(SizeOfTheData); // Here i should write the size of data, but how can I get it?
if (bytesRead == 0)
return;
byte[] data = new byte[bytesRead];
dataReader.ReadBytes(data);
サーバー側でこれを実行しようとしましたが、これは良い解決策ではないと思います:
byte[] data = SomeData();
byte[] length = System.Text.Encoding.ASCII.GetBytes(data.Length.ToString());
// Send the length of the data
serverSocket.Send(length);
// Send the data
serverSocket.Send(data);
私の質問は、長さとデータを同じパケットで送信するにはどうすればよいですか? また、クライアント側でそれを適切に処理するにはどうすればよいですか?