By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

BeginReceive Async network socket example

P: n/a
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.buf fer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.Buf ferSize,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
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Mark Smith <sh******@yahoo.com.au> wrote:
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.buf fer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.Buf ferSize,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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.