Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Asynchronous stream read/write
    primarykey
    data
    text
    <p>I have been trying to solve this "Concurrent Programming" exam exercise (in C#):</p> <blockquote> <p>Knowing that <code>Stream</code> class contains <code>int Read(byte[] buffer, int offset, int size)</code> and <code>void Write(byte[] buffer, int offset, int size)</code> methods, implement in C# the <code>NetToFile</code> method that copies all data received from <code>NetworkStream net</code> instance to the <code>FileStream file</code> instance. To do the transfer, use asynchronous reads and synchronous writes, avoiding one thread to be blocked during read operations. The transfer ends when the <code>net</code> read operation returns value 0. To simplify, it is not necessary to support controlled cancel of the operation.</p> </blockquote> <pre><code>void NetToFile(NetworkStream net, FileStream file); </code></pre> <p>I've been trying to solve this exercise, but I'm struggling with a question related with the question itself. But first, here is my code:</p> <pre><code>public static void NetToFile(NetworkStream net, FileStream file) { byte[] buffer = new byte[4096]; // buffer with 4 kB dimension int offset = 0; // read/write offset int nBytesRead = 0; // number of bytes read on each cycle IAsyncResult ar; do { // read partial content of net (asynchronously) ar = net.BeginRead(buffer,offset,buffer.Length,null,null); // wait until read is completed ar.AsyncWaitHandle.WaitOne(); // get number of bytes read on each cycle nBytesRead = net.EndRead(ar); // write partial content to file (synchronously) fs.Write(buffer,offset,nBytesRead); // update offset offset += nBytesRead; } while( nBytesRead &gt; 0); } </code></pre> <p>The question I have is that, in the question statement, is said:</p> <blockquote> <p>To do the transfer, use asynchronous reads and synchronous writes, avoiding one thread to be blocked during read operations</p> </blockquote> <p>I'm not really sure if my solution accomplishes what is wanted in this exercise, because I'm using <code>AsyncWaitHandle.WaitOne()</code> to wait until the asynchronous read completes.</p> <p>On the other side, I'm not really figuring out what is meant to be a "non-blocking" solution in this scenario, as the <code>FileStream</code> write is meant to be made synchronously... and to do that, I have to wait until <code>NetworkStream</code> read completes to proceed with the <code>FileStream</code> writing, isn't it?</p> <p>Can you, please, help me out with this?</p> <hr> <p>[ EDIT 1 ] <strong>Using <em>callback</em> solution</strong></p> <p>Ok, if I understood what <a href="https://stackoverflow.com/users/13279/mitchel-sellers">Mitchel Sellers</a> and <a href="https://stackoverflow.com/users/84167/willvv">willvv</a> replied, I've been counseled to use a callback method to turn this into a "non-blocking" solution. Here is my code, then:</p> <pre><code>byte[] buffer; // buffer public static void NetToFile(NetworkStream net, FileStream file) { // buffer with same dimension as file stream data buffer = new byte[file.Length]; //start asynchronous read net.BeginRead(buffer,0,buffer.Length,OnEndRead,net); } //asynchronous callback static void OnEndRead(IAsyncResult ar) { //NetworkStream retrieve NetworkStream net = (NetworkStream) ar.IAsyncState; //get number of bytes read int nBytesRead = net.EndRead(ar); //write content to file //... and now, how do I write to FileStream instance without //having its reference?? //fs.Write(buffer,0,nBytesRead); } </code></pre> <p>As you may have noticed, I'm stuck on the callback method, as I don't have a reference to the <code>FileStream</code> instance where I want to invoke the "Write(...)" method.</p> <p>Additionally, this is not a thread-safe solution, as the <code>byte[]</code> field is exposed and may be shared among concurrent <code>NetToFile</code> invocations. I don't know how to solve this problem without exposing this <code>byte[]</code> field in the outer-scope... and I'm almost sure it may not be exposed this way.</p> <p>I don't want to use a lambda or anonymous method solution, because that's not in the curriculum of "Concurrent Programing" course.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload