BeginReceive Async network socket example

M

Mark Smith

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.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so
far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
}
} catch (Exception e) {
Console.WriteLine(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
 
J

Jon Skeet [C# MVP]

Mark Smith said:
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.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so
far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
}
} catch (Exception e) {
Console.WriteLine(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.

I'm not entirely sure what you're asking. Calling Read once (or
anything similar) isn't guaranteed to get all the data in one go.
(Imagine you're downloading hundreds of megs - you may well not *need*
to have it all at one point in time.) Therefore you may well have more
data still to read. When a reading method returns 0, that means that
you've reached the end of the stream, or the socket has been closed,
etc - it means you're done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top