File Transfer from one system to another in c#
<p>I Have this c# program which is a client receives the file from the server. Sometimes it works seamlessly. and sometimes it gives an exception in <code>fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);</code>. </p>

<pre><code>ArgumentOutOfRange Exception
Index and count must refer to a location within the buffer.
Parameter name: bytes
</code></pre>

<p>If the value of <code>fileNameLen</code> is <code>8</code> or <code>12</code> then it works properly. Otherwise it will be <code>1330795077</code>. Why is that? Can anyone explain me why is this? please. Here is my code.</p>

<pre><code>        string fileName = string.Empty;
        int thisRead = 0;
        int blockSize = 1024;
        Byte[] dataByte = new Byte[blockSize];
        lock (this)
        {
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\";
            ns.Read(dataByte, thisRead, blockSize);
            int fileNameLen = BitConverter.ToInt32(dataByte, 0);

            fileName = Encoding.ASCII.GetString(dataByte, 4, fileNameLen);
            Stream fileStream = File.OpenWrite(folderPath + fileName);
            fileStream.Write(dataByte, 4 + fileNameLen, (1024 - (4 + fileNameLen)));
            while (true)
            {
                thisRead = ns.Read(dataByte, 0, blockSize);
                fileStream.Write(dataByte, 0, thisRead);
                if (thisRead == 0)
                    break;
            }
            fileStream.Close();
        }
</code></pre>
