I'm programming a client app on WinRT in C# which connects to several servers by TCP. For the TCP connection I use StreamSocket. The Input and Output Strings are then wrapped in a DataWriter and a DataReader. When I connect to more than one server I get the following exception: "Operation identifier is not valid"
This is the code of the method:
private async void read()
{
while (true)
{
uint bytesRead = 0;
try
{
bytesRead = await reader.LoadAsync(receiveBufferSize);
if (bytesRead == 0)
{
OnClientDisconnected(this);
return;
}
byte[] data = new byte[bytesRead];
reader.ReadBytes(data);
if (reader.UnconsumedBufferLength > 0)
{
throw new Exception();
}
OnDataRead(this, data);
}
catch (Exception ex)
{
if (Error != null)
Error(this, ex);
}
new System.Threading.ManualResetEvent(false).WaitOne(10);
}
}
The Stacktrace only shows the reader.LoadAsync(UInt32 count) method as the root of the problem. Each ClientInstance is running in an own task and has it's own DataReader and Stream instance. The "receiveBufferSize" is at 8192 Bytes.
Do you have any idea what the error could be?