专家。
我想分别计算互联网宽带和本地网络宽带。出于这个原因,我想我可以使用原始套接字来获取所有数据包流量,并且在IP头中使用目标地址和源地址我认识到这个数据包是互联网数据包或本地数据包或BroadCast和....
然后,我可以将每个数据包的长度分别添加到变量。
你认为这是真的????
这是我的计划:
static Stopwatch Time = new Stopwatch();
static IPSegment ip;
static void Main(string[] args)
{
double SumInternet = 0;
double LocalUsage = 0;
Time.Start();
IPAddress MyIp = NetworkManagement.GetIPAddress();
ip = new IPSegment(MyIp.ToString(), NetworkManagement.GetSubnetMask(MyIp).ToString());
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(" ------------------------------- \n| IP Information : {0} \n| BroadCast: {1} \n| BroadcastAddress: {1} \n| NetworkAddress: {2} \n| NumberOfHosts: {3}\n|\n", MyIp.ToString(), ip.BroadcastAddress.ToIpString(), ip.NetworkAddress.ToIpString(), ip.NumberOfHosts.ToIpString());
foreach (uint host in ip.Hosts())
{
Console.WriteLine("| " + host.ToIpString());
}
Console.WriteLine(" -------------------------------");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
NetworkManagement.SetHosts(ip);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
Console.WriteLine("IP Address: {0} is binding . . . \n", MyIp);
sock.Bind(new IPEndPoint(MyIp, 0));
byte[] outValue = new byte[4] { 0, 0, 0, 0 };
byte[] inValue = new byte[4] { 1, 0, 0, 0 };
sock.IOControl(IOControlCode.ReceiveAll, inValue, outValue);
while (true)
{
try
{
byte[] buffer = new byte[sock.ReceiveBufferSize];
int count = sock.Receive(buffer);
IPHeader hdr = new IPHeader(buffer, count);
Console.WriteLine("IP : {0} -> {1} HeaderLen: {2} Byte TotalLen: {3} Byte", hdr.SourceAddress.ToString(), hdr.DestinationAddress, (Convert.ToUInt16(hdr.HeaderLength)) , (Convert.ToUInt16(hdr.TotalLength)));
double volumn = (Convert.ToDouble(hdr.TotalLength));
if (!IsPrivateIp(hdr.DestinationAddress.ToString()) || !IsPrivateIp(hdr.SourceAddress.ToString()))
{
SumInternet += (double)volumn;
}
else
{
LocalUsage += (double)volumn;
}
if (hdr.ProtocolType == Protocol.TCP)
{
TCPHeader tcpheader = new TCPHeader(buffer, count);
Console.WriteLine("TCP :{0} -> {1} Len: {2} Byte", tcpheader.SourcePort, tcpheader.DestinationPort, tcpheader.HeaderLength + tcpheader.HeaderLength);
}
else if (hdr.ProtocolType == Protocol.UDP)
{
UDPHeader tcpheader = new UDPHeader(buffer, count);
Console.WriteLine("UDP :{0} -> {1} Len: {2} Byte", tcpheader.SourcePort, tcpheader.DestinationPort, tcpheader.Length);
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nInternet Usage : {0:0.00} Byte == {1:0.00} Kb ", SumInternet, SumInternet / 1024);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Local Usage : {0:0.00} Byte == {1:0.00} Kb ", LocalUsage, LocalUsage / 1024);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("All Use: {0:0.00} KB ", SumInternet + LocalUsage);
TimeSpan ts = Time.Elapsed;
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("Spend Time: {0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.Write("Internet Speed: {0:0.00} KB/S \n\n", SumInternet / ts.TotalSeconds);
Console.ForegroundColor = ConsoleColor.White;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Error message : {0} | Source: {1} ", ex.Message, ex.Source);
Console.ForegroundColor = ConsoleColor.White;
}
}
}