Dear Readers,
I am in the process of implementing a non-blocking ASYNC TCP client
using the .Net socket class. I am a little confused by the MSDN
example. I have pasted the code here.
private static void ReceiveCallback ( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocke t;
// Read data from the remote device.
int bytesRead = client.EndRecei ve(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so
far.
state.sb.Append (Encoding.ASCII .GetString(stat e.buffer,0,byte sRead));
// Get the rest of the data.
client.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0,
new AsyncCallback(R eceiveCallback) , state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Lengt h > 1) {
response = state.sb.ToStri ng();
}
}
} catch (Exception e) {
Console.WriteLi ne(e.ToString() );
}
}
In the callback if the number of bytes read is zero the else branch is
used.
What conditions do you expect here? Ie will I come through here once
the reads finished or is this branch more to catch the remote end
point hanging up?
Could someone explain this a better as all my data appears to be read
correctly in the first branch.
Thanks
Mark