Note that there are some explanatory texts on larger screens.

plurals
  1. PONetwork stream stuck on stream.Read(byte[] byte, int offset, int size)
    primarykey
    data
    text
    <p>What I'm trying to do is to received a large number of bytes (about 5MB data) sent from the client side</p> <p>Below is the code where data(byte[]) is received</p> <pre><code> byte[] receivedBytesRaw = new byte[4000]; //first, initialize network stream NetworkStream stream = client.GetStream(); //The bytesNeeded is the size of bytes which is a protocol sent by the client side indicating the size of byte which will be sent int bytesNeeded = 4000; int bytesReceived = 0; do { int bytesRead = stream.Read(receivedBytesRaw, bytesReceived, bytesNeeded - bytesReceived); networkValidation.addBytesToList(receivedBytesRaw, ref receivedBytes); bytesReceived += bytesRead; } while (bytesReceived &lt; bytesNeeded); </code></pre> <p>But now I'm stuck on a problem:</p> <p>Everytime when data arrives, the do while loop loops for the first time, and the return value (i) is 26, then it loops again, this time, when it goes to <code>" i = stream.Read(receivedBytesRaw, 0, receivedBytesRaw.Length);"</code>, the program seems waiting for the client side to send data and have no response, also, when I check "receivedBytesRaw", the data was incomplete, only the first 13 bytes was received, the remaining space in the byte array remains null, and the stream.DataAvailable is false</p> <p>Why the server side received incomplete data? Note: when I try to send small data (a string), it's ok</p> <p>=====================================================================</p> <p>Edited</p> <p>Below is the client side code which sends data:</p> <pre><code> private int sendData(byte[] dataSend, string IP, int portNumber) { TcpClient clientSide = new TcpClient(); int result = -1; try { clientSide.Connect(IP, portNumber); } catch (Exception ex) { return 2; } NetworkStream netStream = clientSide.GetStream(); if (netStream.CanWrite) { byte[] replyMsg = new byte[1024]; netStream.Write(dataSend, 0, dataSend.Length); netStream.Flush(); result = 0; } else { result = 1; } return result; } </code></pre>
    singulars
    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.
    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