I'm using a StreamSocket in my WP8.1 (xaml) app and when reading data it sometimes returns 0.
my code looks like this:
var stream = _streamSocket.InputStream.AsStreamForRead(BufferSize);
var buffer = new byte[BufferSize];
while (true)
{
var readCount = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
if (readCount <= 0)
throw new InvalidOperationException("No data to read in socket stream"); <---- error happens here
// process data and stop when marker in data is reached
}
my app sends a request to an http like server then expects to receive a response in the code above.
my question is: why does this happen ?
I recently switch from DataReader to standard .Net streams (using AsStreamForRead) and before the switch I wasn't getting this kind of error.
code before looked like this:
using (var dataReader = new DataReader(socket.InputStream))
{
dataReader.InputStreamOptions = InputStreamOptions.Partial;
await dataReader.LoadAsync(BufferSize).AsTask().ConfigureAwait(false);
// read as much as dataReader.UnconsumedBufferLength bytes....
dataReader.DetachStream();
}
can it happen that Stream.ReadAsync() doesn't wait for data to come in the socket
while DataReader/LoadAsync() does ?
No comments:
Post a Comment