Hi guys,

I currently have a custom network protocol I have written that uses the NetworkStream class. Within my protocol I have implemented a way to detect data that also works as a timeout. This seemed to be working fine until my client uploads an image of approx 24kb+ and then my timeout method fails and returns which then causes the upload to fail.

Does anybody have any pointers as to where I am going wrong? The code I have is below:

        public bool WaitForData(int TimeoutSeconds)
            if (TimeoutSeconds < 0) // No timeout
                while (__netstream.DataAvailable == false) Thread.Sleep(1);
                return true;
                Stopwatch timer = new Stopwatch();

                    if (__netstream.DataAvailable) return true;
                while (timer.Elapsed.Seconds < TimeoutSeconds);


                return false;

The way this code is/was used is:

        public byte[] ReadBytes(int size) { return ReadBytes(size, null); }
        public byte[] ReadBytes(int size, Action<int> ProgressBar_Callback)
            byte[] barr = new byte[size];

            for (int i = 0; i < barr.Length; )
                // the source of problems uploading invoices over approx 24kb:
                //if (WaitForData(__timeout) == false) throw new Exception("RawSocket ReadBytes() Timed Out: Total bytes read: " + i);

                int read = __netstream.Read(barr, i, Math.Min(barr.Length - i, 8192));

                i += read;

                if (i % 10 == 0 && ProgressBar_Callback != null) ProgressBar_Callback(i * 100 / size);

            if (ProgressBar_Callback != null) ProgressBar_Callback(100);

            return barr;

Any ideas on improving? I am currently relying on the client not disconnecting during the upload.


Hi, if you don't want to rely on the client that he/she doesn't disconnects during the upload, then possibly you may think of developing your own service that would re-run this app if even someone closes OR provide some access controls that would stop anyone except the Admin to disconnect from your application.