《用c#写程序怎么知道与网络传输了多少字节,用于计流量》由会员分享,可在线阅读,更多相关《用c#写程序怎么知道与网络传输了多少字节,用于计流量(11页珍藏版)》请在金锄头文库上搜索。
1、用用 C#写程序怎么知道与网络传输了多少字节,用于计流量写程序怎么知道与网络传输了多少字节,用于计流量30补充: 用 C#写程序怎么知道与网络传输了多少字节,给段计流量代码看下,用于计流量我来回答回答回答(1)东华 3 级 2011-11-14using System;using System.Text;using System.Net;using System.Net.Sockets;using System.Runtime.InteropServices;namespace UpdateTester/ / Monitor 的摘要说明。/ public class Monitorpublic
2、 delegate void NewPacketEventHandler(Monitor m, Packet p);public event NewPacketEventHandler NewPacket;private Socket m_Monitor;private IPAddress m_Ip;private byte m_Buffer = new byte65535;private const System.Int32 IOC_VENDOR = 0x18000000;private const int IOC_IN = -2147483648;private const int SIO
3、_RCVALL = IOC_IN IOC_VENDOR 1;private const int SECURITY_BUILTIN_DOMAIN_RID = 0x20;private const int DOMAIN_ALIAS_RID_ADMINS = 0x220;public System.Net.IPAddress IPget return m_Ip; public byte Bufferget return m_Buffer; public Monitor()/ TODO: 在此处添加构造函数逻辑/public Monitor(IPAddress IpAddress)if (!(Envi
4、ronment.OSVersion.Platform = PlatformID.Win32NT) private DateTime m_Time;private int m_Version;private int m_HeaderLength;private Precedence m_Precedence;private Delay m_Delay;private Throughput m_Throughput;private Reliability m_Reliability;private int m_TotalLength;private int m_Identification;pri
5、vate int m_TimeToLive;private Protocol m_Protocol;private byte m_Checksum;private string m_SourceAddress;private string m_DestinationAddress;private int m_SourcePort;private int m_DestinationPort;public Packet()/ TODO: 在此处添加构造函数逻辑/ public Packet(byte raw):(byte raw, DateTime time)/ / Packet(raw, Dat
6、eTime.Now);/ public Packet(byte raw, DateTime time)if (raw=null) throw new ArgumentNullException();if (raw.Length 5);this.m_Delay = (Delay)(raw1 this.m_Throughput = (Throughput)(raw1 this.m_Reliability = (Reliability)(raw1 this.m_TotalLength = raw2 * 256 + raw3;if ( ! (this.m_TotalLength = raw.Lengt
7、h) throw new ArgumentException(); / invalid size of packet;this.m_Identification = raw4 * 256 + raw5;this.m_TimeToLive = raw8;m_Protocol = (Protocol)raw9;m_Checksum = new byte2;m_Checksum0 = raw11;m_Checksum1 = raw10;trym_SourceAddress = GetIPAddress(raw, 12);m_DestinationAddress = GetIPAddress(raw,
8、 16);catch (Exception e)throw;if (m_Protocol = Protocol.Tcp | m_Protocol = Protocol.Udp)m_SourcePort = rawm_HeaderLength * 256 + rawm_HeaderLength + 1;m_DestinationPort = rawm_HeaderLength + 2 * 256 + rawm_HeaderLength + 3;else m_SourcePort = -1;m_DestinationPort = -1;public string GetIPAddress(byte
9、 bArray, int nStart) byte tmp = new byte4;if (bArray.Length nStart + 2)tmp0 = bArraynStart;tmp1 = bArraynStart + 1;tmp2 = bArraynStart + 2;tmp3 = bArraynStart + 3;return tmp0 + “.“ + tmp1 + “.“ + tmp2 + “.“ + tmp3;public int TotalLengthget return m_TotalLength; public DateTime Timeget return this.m_
10、Time; public Protocol Protocolget return this.m_Protocol; public string SourceAddressget return this.m_SourceAddress; public string Source get if ( m_SourcePort != -1 ) return SourceAddress.ToString() + “:“ + m_SourcePort.ToString();else return SourceAddress.ToString(); public string Destinationget
11、if (this.m_DestinationPort != -1) return DestinationAddress.ToString() + “:“ + m_DestinationPort.ToString();else return DestinationAddress.ToString();public string DestinationAddressgetreturn m_DestinationAddress;-在主程序里private Monitor m_PacketMonitors;private ArrayList m_Packets;private System.Windo
12、ws.Forms.StatusBar statusBar1;private int m_PacketsSize;执行方法中private void StartMonitor() IPAddress hosts = Dns.Resolve(Dns.GetHostName().AddressList;if (hosts.Length = 0) throw new NotSupportedException(“This computer does not have non-loopback interfaces installed!“);for (int i=0; ihosts.Length; i+
13、)m_PacketMonitors = new Monitor1;m_Packets = new ArrayList();m_PacketMonitors0 = new Monitor(hosts0);/ 添加代理,每次有新的 packet 到时都出发下面哪个动作m_PacketMonitors0.NewPacket+=new Monitor.NewPacketEventHandler(this.OnNewPacket);m_PacketMonitors0.Start();/ 这个方法用于把 packet 显示到一个地方private void OnNewPacket(Monitor m, P
14、acket p)m_Packets.Add(p);m_PacketsSize += p.TotalLength;trytxtLog.Text += p.Time.ToString()+p.Protocol.ToString()+p.Source.ToString()+p.Destination.ToString()+p.TotalLength.ToString();catch (Exception e)MessageBox.Show(e.Message);statusBar1.Text = String.Format(“Intercepted 0 packet(s) 1 bytes“, m_Packets.Count, m_PacketsSize);