I have problem with reading buffer, when reading is not completed.
If the received XML data is valid, then I have no problem. But when the received XML data is not correct then I am waiting for the second part of the data, and there I get an error.
My code looks like:
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);
}
How can I get the rest of the data and how do I use offset correctly?
I am getting this error:
specified argument was out of the range of valid values. parameter name : size
If you use an offset, the data you receive will be stored in the buffer starting at the specified offset, so you need an array of size offset + length. In your case just adjust the length to correctly indicate how many bytes you can store (
buffer.Length - currentDataSize):