2012-01-16 5 views
2

読み込みバッファに問題があり、読み込みが完了していない場合。BeginReceiveで複数の部品を受け取る方法は?

受信したXMLデータが有効な場合は問題ありません。しかし、受け取ったXMLデータが正しくなければ、データの2番目の部分を待っています。そこでエラーが発生します。

私のコードは次のようになります。

int currentDataSize = socket.EndReceive(iar); 
string currentData = convertToString(buffer, currentDataSize); 

if (IsValidXml(currentData)) 
{ 
    //Here I am parsing the xml data and writing into the sql db. 
    runParseWorker(currentData);  

    //Here I am sending the xml-data to all clients. 
    runSendWorker(currentData); 

    //Here I am calling the BeginRecieve Method again. 
    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, 
         new AsyncCallback(dataRecievedCallback), null); 
} 
else 
{ 
    //It gives error when I use offset.. 
    socket.BeginReceive(buffer, currentDataSize, buffer.Length, SocketFlags.None, 
         new AsyncCallback(dataRecievedCallback), buffer); 
} 

にはどうすれば残りのデータを取得することができますし、どのように私は正しくオフセット使うのですか?あなたはの配列を必要とするので、オフセットあなたがオフセット使用する場合は、受信データを指定で始まるバッファに格納されます

specified argument was out of the range of valid values. parameter name : size 

答えて

2
socket.BeginReceive(buffer, currentDataSize, buffer.Length, SocketFlags.None, 
         new AsyncCallback(dataRecievedCallback), buffer); 

私はこのエラーを取得していますサイズオフセット+長さ。あなたのケースでは、単に長さを正しくあなたが(buffer.Length - currentDataSize)を格納することができますどのように多くのバイト数を示すために調整します。

socket.BeginReceive(buffer, currentDataSize, buffer.Length - currentDataSize, SocketFlags.None, 
         new AsyncCallback(dataRecievedCallback), buffer); 
+0

はあなたに壊れありがとうを、もう一つの問題は、それを行うには良い方法はありますか? – Racooon

+0

一般的にデータは任意の数のチャンクで*受信できます*すべてのデータを受信するまでは 'BeginReceive'を呼び出さなければなりません。 – BrokenGlass

関連する問題