C#.net 套接字上传速率
我有一个类,它使用套接字通过网络异步发送和接收数据:
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);
}
}
我的问题是,如何在发送数据时计算上传速率? .Net 有办法指示特定套接字上的下载/上传速率吗?
我正在使用 C# 4.0
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
操作系统的工作就是提供此类信息。启动 Perfmon.exe,选择性能监视器。右键单击图形区域,然后单击添加计数器。查看“网络接口”部分。每秒发送的字节数为您提供了良好的速率指标。或当前带宽。
这也可以从 C# 获得,使用 PerformanceCounter 类。选择正确的网络接口可能很麻烦,但请记住,通过性能监视器可以轻松获得信息。用户通常只关心下载进度,她几乎无能为力来加快下载速度。简单的 ProgressBar 就可以很好地完成这项工作。
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.