views:

37

answers:

1

I have a class that uses sockets to send and receive data asynchronously over the network:

class Client
{
    private Socket mSocket;
    /*
    ...
    */
    public void SendPacket(byte[] data)
    {
        mSocket.BeginSend(data, 0, data.Length, SocketFlags.None, OnSent, null);
    }

    private void OnSent(IAsyncResult ar)
    {
        mSocket.EndSend(ar);
    }
}

My question is, how can I calculate the upload rate, while the data is being sent? Does .Net have a way to indicate the download / upload rate over a specific socket?

I am using C# 4.0

A: 

It is the job of the operating system to provide that kind of info. Fire up Perfmon.exe, select Performance Monitor. Right-click the graph area and click Add Counters. Look in the "Network Interface" section. Bytes sent/sec gives you a good rate indicator. Or Current Bandwidth.

This is also available from C#, use the PerformanceCounter class. Selecting the right network interface can be a hassle, just keep in mind that the info is available at your fingertips with the performance monitor. The user typically is only interested in how far the download progressed, there very little she can do to make it faster. Simple ProgressBar will do that job just fine.

Hans Passant
Thanks for the answer. I will have a look at PerformanceCounter class. Just to mention, it's "perfmon.exe".
Bobos
OK so, I've found that the class provides global down/up rates over an AppDomain (and not over each Socket in the AppDoman). This is not what i really need. So I will just show a progress bar, as your advice. Thank's for your help!
Bobos