PDA

View Full Version : حرفه ای: چگونه میزان مصرف اینترنت یک کلاینت را به دست بیاوریم؟



amin_sltny
شنبه 24 آبان 1393, 09:20 صبح
درود بر همه

دوستان گرامی من یه کلاس دارم به شکل زیره و کارش محاسبه میزان مصرف کارت های شبکه اس و بلوتوث و ... در واقع میزان send byte و recived byte اون ها را به دست میاره.



public sealed class BoundWidth
{
const int numberOfIterations = 10;
protected static long _PrimaryDataSend;
protected static long _PrimaryDataRecive;
protected static long _dataSend;
protected static long _dataReceive;
protected static long _speed;
protected static long _download;
protected static long _upload;
protected static NetworkInterface[] nicArr;
protected static NetworkInterface _netInterface;
protected static IPv4InterfaceStatistics _interfaceStats;
protected static Thread ThreadBoundWeight;
protected static string _interfaceType;


public static double bytesSend { get { return _dataSend - _PrimaryDataSend; } }
public static double bytesReceive { get { return _dataReceive - _PrimaryDataRecive; } }
public static double DownloadSpeed { get { return _download; } }
public static double UploadSpeed { get { return _upload; } }
public static double Speed { get { return _speed; } }


public static double UsageBoundWidth { get { return (bytesSend) + (bytesReceive); } }


private static void Initializing(string netName)
{
_dataReceive = 0;
_dataSend = 0;
_download = 0;
_speed = 0;
_upload = 0;
_interfaceType = "";
nicArr = NetworkInterface.GetAllNetworkInterfaces();


foreach (NetworkInterface net in nicArr)
{
if (net.Name == netName)
{
_netInterface = net;
break;
}
}


if (_netInterface == null)
{
_netInterface = nicArr[0];
}
_interfaceStats = _netInterface.GetIPv4Statistics();
_PrimaryDataSend = _interfaceStats.BytesSent / 1024;
_PrimaryDataRecive = _interfaceStats.BytesReceived / 1024;
}




public BoundWidth(string NetWorkCardName)
{
Initializing(NetWorkCardName);
ThreadBoundWeight = new Thread(new ThreadStart(delegate { Calculatedbandwidth(); })) { IsBackground = true };
ThreadBoundWeight.Start();
}


protected void Calculatedbandwidth()
{
while (true)
{
_interfaceStats = _netInterface.GetIPv4Statistics();
int bytesSentSpeed = (int)((_interfaceStats.BytesSent / 1024) - _dataSend);
int bytesReceivedSpeed = (int)((_interfaceStats.BytesReceived / 1024) - _dataReceive);


// Update the labels
_speed = _netInterface.Speed / 1024; ;
_interfaceType = _netInterface.NetworkInterfaceType.ToString();
_dataReceive = _interfaceStats.BytesReceived / 1024;
_dataSend = _interfaceStats.BytesSent / 1024;
_upload = bytesSentSpeed;
_download = bytesReceivedSpeed;
Thread.Sleep(100);
}
}


public static string[] GetAllNetworkInterfacesName()
{
if (nicArr == null)
nicArr = NetworkInterface.GetAllNetworkInterfaces();
string[] strname = new string[nicArr.Length];
for (int i = 0; i < nicArr.Length; i++)
strname[i] = nicArr[i].Name;
return strname;
}


public static void SetPrimary()
{
Initializing(_netInterface.Name);
}
}



من این کلاس را برای محاسبه مصرف اینترنت کلاینت ها به کار بردم اما یه مشکلی داره و اونم اینه که میزان ترافیک خود شبکه را هم جزء مصرف اینترنت میاره!!! لطفا راهنماییم کنید باید چه کنم ممنون

omid nasri
شنبه 24 آبان 1393, 11:40 صبح
تو گوگل سرچ کن مطما باش گیر میاری، اگه نتونستی بگو برات ارسال کنم.( ایمیل شخصی پخ بزار )

amin_sltny
دوشنبه 26 آبان 1393, 08:09 صبح
تو گوگل سرچ کن مطما باش گیر میاری، اگه نتونستی بگو برات ارسال کنم.( ایمیل شخصی پخ بزار )

کسی نبود کمک کنه؟!!!!!!! :)

hamid_hr
دوشنبه 26 آبان 1393, 11:05 صبح
بنظر من
شما ای پی gateway مربوط به سیستم رو بدست بیارید
بعد میزان send , recive بین سیستم و این ایپی رو بگیرید میشه میزان مصرف اینترنت

amin_sltny
دوشنبه 26 آبان 1393, 16:15 عصر
بنظر من
شما ای پی gateway مربوط به سیستم رو بدست بیارید
بعد میزان send , recive بین سیستم و این ایپی رو بگیرید میشه میزان مصرف اینترنت


دوست گرامی با با استفاده از قطعه کد زیر Gateway سیستمم را به دست آوردم ولی نمی دونم چه طوری میزان send و recive بین سیستم و این آی پی را به دست بیارم!!!! :متفکر:


public static IPAddress GetDefaultGateway()
{
NetworkInterface[] listcard = NetworkInterface.GetAllNetworkInterfaces();
var card = listcard[3];
if (card == null) return null;
var address = card.GetIPProperties().GatewayAddresses.FirstOrDef ault();
return address.Address;

}