private static void GetNetBag() { NS = new NativeSocket2(IPAddress.Parse(IP)); NS.IsStart = true; NS.OnIPPacketCapure = (IPPacket tp) => { BagCount++; if (tp.SrcAddr.ToString() == IP) { //源地址是本机-从本机发出 lock (netProcesses) { bool _in = false; foreach (var item in netProcesses) { int inPort = item.Ports.Where(x => x.Port == tp.SrcPort).Count();; if (inPort > 0) { item.UpBag++; NowBag++; _in = true; //item.Upload += tp.DataLen; //item.FlowCount += tp.DataLen; } } if (!_in) { NowBadBag++; } } } if (tp.DestAddr.ToString() == IP) { //目标地址是本机-本机接收 lock (netProcesses) { bool _in = false; foreach (var item in netProcesses) { int inPort = item.Ports.Where(x => x.Port == tp.DestPort).Count();; if (inPort > 0) { item.DownBag++; NowBag++; _in = true; //item.DownLoad += tp.DataLen; //item.FlowCount += tp.DataLen; } } if (!_in) { NowBadBag++; } } } }; Task.Factory.StartNew(() => { NS.Capture(); }); }
private void CaptureInit() { _rs = new NativeSocket2(IPAddress.Parse(IP)); _rs.IsStart = true; _rs.OnIPPacketCapure = (IPPacket tp) => { this.Invoke(new Action(() => { try { if (tp.SrcAddr.ToString() == IP) { //源地址是本机-从本机发出 lock (netProcesses) { foreach (var item in netProcesses) { int inPort = item.Ports.Where(x => x.Port == tp.SrcPort).Count();; if (inPort > 0) { item.UpBag++; if (item.UpBag > 1000) { item.UpBag = 0; } NowBag++; } } } } if (tp.DestAddr.ToString() == IP) { //目标地址是本机-本机接收 lock (netProcesses) { foreach (var item in netProcesses) { int inPort = item.Ports.Where(x => x.Port == tp.DestPort).Count();; if (inPort > 0) { item.DownBag++; if (item.DownBag > 1000) { item.DownBag = 0; } NowBag++; } } } } } catch { } })); }; }