Calling BeginRead from a NetworkStream

21,198

Basically, when you call the Begin* method on an asynchronous operation, there needs to be a call to a corresponding End* statement (for more detailed information, see the Asynchronous Programming Overview on MSDN, specifically the section titled "Ending an Asynchronous Operation").

That said, you generally want to pass a method/anonymous method/lambda expression which will do one or two things:

1) Call the corresponding End* method, in this case, Stream.EndRead. This call will not block when called because the callback will not be called until the operation is complete (note that if an exception occurred during the async call then this exception will be thrown when the End* method is called).

2) Possibly start a new asynchronous call. In this case, it's assumed you'll want to read more data, so you should start a new call to Stream.BeginRead

Assuming you want to do #2, you can do the following:

// Declare the callback.  Need to do that so that
// the closure captures it.
AsyncCallback callback = null;

// Assign the callback.
callback = ar => { 
    // Call EndRead.
    int bytesRead = myNetworkStream.EndRead(ar);

    // Process the bytes here.


    // Determine if you want to read again.  If not, return.
    if (!notMoreToRead) return;

    // Read again.  This callback will be called again.
    myNetworkStream.BeginRead(buffer, offset, count, callback, null);
};

// Trigger the initial read.
myNetworkStream.BeginRead(buffer, offset, count, callback, null);

However, if you are using .NET 4.0, this becomes much easier using the FromAsync method on the TaskFactory class.

Share:
21,198
JMK
Author by

JMK

Software developer currently living in Belfast, jack of a couple of trades, master of none!

Updated on July 18, 2022

Comments

  • JMK
    JMK almost 2 years

    Ok I want to connect to a Socket and read a network stream using the System.Net.Sockets.NetworkStream class. This is what I have so far:

    NetworkStream myNetworkStream;
    Socket socket;
    
    socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, 
        ProtocolType.IPv4);
    socket.Connect(IPAddress.Parse("8.8.8.8"), 8888);
    
    myNetworkStream = new NetworkStream(socket);
    
    byte[] buffer = new byte[1024];
    int offset = 0;
    int count = 1024;
    
    myNetworkStream.BeginRead(buffer, offset, count, ??, ??);
    

    Now I need an AsyncCallback and an Object state to complete my BeginRead method but I'm not even sure if this is going to work. I'm a bit lost at this point! Where do I need to go from here?