TCPクライアントに何らかの問題が発生しました。このサービスはサーバーに接続し、受信して90%処理されたメッセージを受信します。ちょっとしたデバッグの後、私は「データのパケット全体を読み取ることができません!」というメッセージが表示されることがありました。エラー(コード内)C#TCPクライアントで非同期処理エラーが発生しました
サーバーの送信内容を制御できません。 私は、完全なメッセージを受け取っていないと考えています(長いメッセージでは失敗しますが、ボリュームでは不安です)。
私がこれを説明できる最良の方法は何ですか、またはその他のヒントです。私はすべての行動と問題がどこにあるのか私の受信方法を持っています。
私は、これはTCPと私の最初の試みであるので、あなたはTCPサーバから1回の読み取りに全体「メッセージ」を取得することを前提とすることはできません
public void Receive(IAsyncResult ar)
{
if (ar != null)
{
try
{
byte[] mLenBytes = (byte[])ar.AsyncState;
byte[] mDataBytes = null;
int bytes_read = mTcpClient.Client.EndReceive(ar);
if (bytes_read != 4)
throw new Exception("Unable to read whole header!");
int len = mLenBytes[3] + ((mLenBytes[2] + (mLenBytes[1] + (mLenBytes[0] << 8)) << 8) << 8);
mDataBytes = new byte[len];
bytes_read = mTcpClient.Client.Receive(mDataBytes, len, SocketFlags.None);
if (bytes_read != len)
throw new Exception("Unable to read whole packet of data!" + "Expected " + len + " Got " + Convert.ToString(bytes_read) + "\r\n" + (System.Text.Encoding.ASCII.GetString(mDataBytes)));
//This is the error that is raised!.
// raise an event
PhoneBoxEventArgs e1 = new PhoneBoxEventArgs(System.Text.Encoding.UTF8.GetString(mDataBytes));
Console.WriteLine("Data received is = " + e1.Data);
OnPassEvent(e1);
}
catch (Exception ex)
{
}
}
byte[] mLenBytes_new = new byte[4];
mTcpClient.Client.BeginReceive(mLenBytes_new, 0, 4, SocketFlags.None, new AsyncCallback(Receive), mLenBytes_new);
}
はい私はヘッダーを取得するように変更しましたが、私は合計を受け取っていないうちに私は試し続けます!ヒントをありがとう。 – user685590