3

Solved:

I'm using WCF to transfer files by streaming. The client calls a method in the service, then the service take the file from the client. On the way, I'm sending back by CallBack the speed.

My problem is that I can't determine which speed am I calculating. When the service takes the file from the client, it uses download speed. However, when the client sends the file it is the upload speed. Which one do I need to calculate, and how?

Not solved yet:

When the client calls the service's method (and gives it the stream with a reference to the file),it takes TOO long (depends on the size of the file) from the time the client calls the method until the service's method starts to activate. Why does this happen? A one Gigabyte file will take forever.

*From the time the service's method starts, the all thing works fine with no problems. So showing the service is a waste of time.

(client)

Stream TheStream = File.OpenRead(@"C:\BigFile.rar");
Service1.GiveAFile(TheStream);

Thanks.

John Arlen
  • 6,539
  • 2
  • 33
  • 42
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • Download speed and upload speed are the same for any given transfer (ignoring buffering, of course). – Chris Shain May 08 '12 at 21:35
  • 1
    The speed you are calculating is *the one that is slowest* (if we're talking about bandwidth). On a DSL line, that's generally the upload speed. The transfer rate will never exceed the slowest of the upload/download speeds. – Robert Harvey May 08 '12 at 21:37
  • In which case, they are not the same thing? There must be one. – Stav Alfi May 08 '12 at 21:38
  • 1
    @StavAlfi that's true, but that has nothing to do with the speed. There are different "scales" for measuring that and what you most often get from your provider is a quote on the burst rate, not on your actual throughput. – Kiril May 08 '12 at 21:45
  • Possible duplicate of http://stackoverflow.com/q/566139/879420 – James Johnson May 08 '12 at 21:45
  • Okay thanks. I will love to get some help about my second problem as well. – Stav Alfi May 08 '12 at 21:58

2 Answers2

3

Source : How to calculate network bandwidth speed in c#

CODE:
using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace InterfaceTrafficWatch
{
    /// <summary>
    /// Network Interface Traffic Watch
    /// by Mohamed Mansour
    /// 
    /// Free to use under GPL open source license!
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Timer Update (every 1 sec)
        /// </summary>
        private const double timerUpdate = 1000;

        /// <summary>
        /// Interface Storage
        /// </summary>
        private NetworkInterface[] nicArr;

        /// <summary>
        /// Main Timer Object 
        /// (we could use something more efficient such 
        /// as interop calls to HighPerformanceTimers)
        /// </summary>
        private Timer timer;

        /// <summary>
        /// Constructor
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            InitializeNetworkInterface();
            InitializeTimer();
        }

        /// <summary>
        /// Initialize all network interfaces on this computer
        /// </summary>
        private void InitializeNetworkInterface()
        {
            // Grab all local interfaces to this computer
            nicArr = NetworkInterface.GetAllNetworkInterfaces();

            // Add each interface name to the combo box
            for (int i = 0; i < nicArr.Length; i++)
                cmbInterface.Items.Add(nicArr[i].Name);

            // Change the initial selection to the first interface
            cmbInterface.SelectedIndex = 0;
        }

        /// <summary>
        /// Initialize the Timer
        /// </summary>
        private void InitializeTimer()
        {
            timer = new Timer();
            timer.Interval = (int)timerUpdate;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        /// <summary>
        /// Update GUI components for the network interfaces
        /// </summary>
        private void UpdateNetworkInterface()
        {
            // Grab NetworkInterface object that describes the current interface
            NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];

            // Grab the stats for that interface
            IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();

            // Calculate the speed of bytes going in and out
            // NOTE: we could use something faster and more reliable than Windows Forms Tiemr
            //       such as HighPerformanceTimer http://www.m0interactive.com/archives/2006/12/21/high_resolution_timer_in_net_2_0.html
            int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
            int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;

            // Update the labels
            lblSpeed.Text = nic.Speed.ToString();
            lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
            lblSpeed.Text = nic.Speed.ToString();
            lblBytesReceived.Text = interfaceStats.BytesReceived.ToString();
            lblBytesSent.Text = interfaceStats.BytesSent.ToString();
            lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
            lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";

        }

        /// <summary>
        /// The Timer event for each Tick (second) to update the UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Tick(object sender, EventArgs e)
        {
            UpdateNetworkInterface();
        }

    }
}
Ehsan Enaloo
  • 341
  • 2
  • 3
  • I use this code and test it on Windows Server 2012. App hows send/receive speed as 13/3 kb/s, but Server's task manager shows 136/24. I.e. the proportion the same but the values are different. Why? – ZedZip Jan 29 '15 at 11:33
  • 1
    @Oleg you need to multiply those values from your code by 8 – ArcadeRenegade Jan 26 '16 at 06:31
0

For the second issue:

Most likely your service is loading the entire file into memory before streaming it back to the client.

You might look at the following question to get an idea of how to properly chunk it.

How can I read/stream a file without loading the entire file into memory?

Community
  • 1
  • 1
NotMe
  • 87,343
  • 27
  • 171
  • 245
  • Thanks for trying but **this isnt helping**. The solution in that topic is **exactly** what I implemented. I gave my client's code of how he make the stream befor sending it to the service. Any other option? – Stav Alfi May 08 '12 at 22:14
  • 1
    Use response.transmitfile(): http://msdn.microsoft.com/en-us/library/12s31dhy(v=vs.100).aspx Instead of sending a stream to your service, just give it the file reference and tell use transmitfile to send it on. TransmitFile doesn't buffer it in memory. – NotMe May 08 '12 at 22:16