TCP で複数のサーバーに接続する C# の WinRT でクライアント アプリをプログラミングしています。TCP 接続には StreamSocket を使用します。次に、入力文字列と出力文字列が DataWriter と DataReader にラップされます。複数のサーバーに接続すると、「操作識別子が無効です」という例外が発生します。
メソッドのコードは次のとおりです。
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);
}
}
Stacktrace は、問題の原因として reader.LoadAsync(UInt32 count) メソッドのみを示しています。各 ClientInstance は独自のタスクで実行され、独自の DataReader および Stream インスタンスを持ちます。「receiveBufferSize」は 8192 バイトです。
エラーが何であるか分かりますか?