Hello everybody,

I was wondering if anybody knows whether or not it is possible to send data too fast over a NetworkStream?

I have an issue with my client/server program, where I am sending large files over normal IP address (Not Local) where everything used to work fine. It would send 1 byte as a hash code for the following byte, and then send the byte of data, a total of 2 bytes per 1 byte of "useful" data. I quickly found out this was causing transfers to become very slow. I have now removed the hash bytes before the data and opted for a checksum of the file instead, however now that I have removed these extra bytes, my program freezes for a while and throws an exception unless I use something like "Thread.Sleep(1)" after every byte sent, which makes everything even slower than sending with a hash code.

Here is a portion of the code. The error however is just the program freezing and the try-catch eventually throwing a "remote party closed connection" after 30 seconds or so.

Method freezing and throwing error:

        DateTime w_trans_started = DateTime.Now;
        DateTime w_last_update = DateTime.Now;
        public void WriteFrame112_GUI(FileStream f, ProgressBar pb, Label percentage, Label timeremaining) // GUI integrated operations
        {
            if (pb != null)
            {
                pb.Value = 0;
                pb.Style = ProgressBarStyle.Blocks;
                pb.Minimum = 0;
                pb.Maximum = 100;
            }

            try
            {
                SC.WriteByte(112); // Frame Identifier

                SC.WriteInteger((int)f.Length); // Write size of file

                w_trans_started = DateTime.Now;
                w_last_update = DateTime.Now;
                for (int i = 0; i < f.Length; i++) // Write file
                {
                    if (i % 1000 == 0) // Update GUI every 1000 bytes sent
                    {
                        if (pb != null)
                        {
                            pb.Value = (int)(((decimal)i / (decimal)f.Length) * 100);
                        }
                        if (percentage != null)
                        {
                            percentage.Text = ((int)(((decimal)i / (decimal)f.Length) * 100)).ToString() + "%";
                        }
                        if (timeremaining != null)
                        {
                            TimeSpan taken = (DateTime.Now.Subtract(w_trans_started));
                            if (taken.Seconds != 0 && i != 0 && DateTime.Now.Subtract(w_last_update).Seconds > 2)
                            {
                                try
                                {
                                    w_last_update = DateTime.Now;
                                    int Hours = (int)((((decimal)taken.Seconds / (decimal)i) * (decimal)(f.Length - i)) / (decimal)3600);
                                    int Minutes = (int)(((((decimal)taken.Seconds / (decimal)i) * (decimal)(f.Length - i)) - ((decimal)Hours * (decimal)3600)) / (decimal)60);
                                    int Seconds = (int)(((((decimal)taken.Seconds / (decimal)i) * (decimal)(f.Length - i)) - ((decimal)Minutes * (decimal)60)));
                                    timeremaining.Text = "Estimated Time Remaining: " + Hours + " Hour(s) " + Minutes + " minute(s) and " + Seconds + " second(s)";
                                }
                                catch { }
                            }
                        }
                        Application.DoEvents();
                    }

                    SC.WriteByte((byte)f.ReadByte());
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message + "\n" + e.StackTrace, "WriteFrame112_GUI"); }

            if (pb != null)
            {
                pb.Value = 100;
                Application.DoEvents();
                Thread.Sleep(50);
                Application.DoEvents();
            }
        }

        // ABOVE CALLS BELOW VIA SC.WRITEBYTE(BYTE)

        public void WriteByte(byte b)
        {
            //WriteHash(b); // This just wrote an int of the hash code
            NS.WriteByte(b);
        }


        Incase you need it, here is the recv method:

        public byte[] ReadFrame112()
        {
            try
            {
                int filelen = SC.ReadInteger(); // File length?

                byte[] data = new byte[filelen];
                for (int i = 0; i < filelen; i++)
                {
                    data[i] = SC.ReadByte();
                }

                return data;
            }
            catch (Exception e) { if (!ServerMode) MessageBox.Show(e.Message + "\n" + e.StackTrace, "ReadFrame112"); return null; }
        }

To answer your question, no it isn't possible to send data too fast.

And it sounds like the problem is on the receiving end, if you are getting a "remote party closed connection" error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.