用c#监控网络流量
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace UpdateTester
...{
/**//**//**//// <summary>
/// Monitor 的摘要说明。
/// </summary>
public class Monitor
...{
public delegate void NewPacketEventHandler(Monitor m, Packet p);
public event NewPacketEventHandler NewPacket;
private Socket m_Monitor;
private IPAddress m_Ip;
private byte[] m_Buffer = new byte[65535];
private const System.Int32 IOC_VENDOR = 0x18000000;
private const int IOC_IN = -2147483648;
private const int SIO_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 IP
...{
get ...{ return m_Ip; }
}
public byte[] Buffer
...{
get ...{ return m_Buffer; }
}
public Monitor()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Monitor(IPAddress IpAddress)
...{
if (!(Environment.OSVersion.Platform == PlatformID.Win32NT) &&
Environment.OSVersion.Version.Major<5)
...{
throw new NotSupportedException(
"This program requires Windows 2000, Windows XP or Windows .NET Server!");
}
m_Ip = IpAddress;
}
public void Start()
...{
if (m_Monitor==null)
...{
try
...{
m_Monitor = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.IP);
m_Monitor.Bind(new IPEndPoint(IP, 0));
m_Monitor.IOControl(SIO_RCVALL, BitConverter.GetBytes(1), null);
m_Monitor.BeginReceive(m_Buffer, 0,
m_Buffer.Length, SocketFlags.None,
new AsyncCallback(OnReceive), null);
}
catch (Exception e)
...{
m_Monitor = null;
throw new SocketException();
}
}
}
public void Stop()
...{
if (m_Monitor!=null)
...{
m_Monitor.Close();
}
m_Monitor = null;
}
public void OnReceive(System.IAsyncResult ar)
...{
try
...{
int received = m_Monitor.EndReceive(ar);
try
...{
if (m_Monitor!=null)
...{
byte[] pkt = new byte[received];
Array.Copy(Buffer, 0, pkt, 0, received);
OnNewPacket(new Packet(pkt, DateTime.Now));
}
}
catch(Exception e)
...{
throw;
}
m_Monitor.BeginReceive(Buffer, 0, Buffer.Length,
SocketFlags.None, new AsyncCallback(OnReceive), null);
}
catch (Exception e)
...{
}
}
protected void OnNewPacket(Packet p)
...{
NewPacket(this, p);
}
}
}
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UpdateTester
...{
public enum Precedence
...{
Routine = 0,
Priority = 1,
Immediate = 2,
Flash = 3,
FlashOverride = 4,
CRITICECP = 5,
InternetworkControl = 6,
NetworkControl = 7
}
public enum Delay
...{
NormalDelay = 0,
LowDelay = 1
}
public enum Throughput
...{
NormalThroughput = 0,
HighThroughput = 1
}
public enum Reliability
...{
NormalReliability = 0,
HighReliability = 1
}
public enum Protocol
...{
Ggp = 3,
Icmp = 1,
Idp = 22,
Igmp = 2,
IP = 4,
ND = 77,
Pup = 12,
Tcp = 6,
Udp = 17,
Other = -1
}
/**//**//**//// <summary>
/// Packet 的摘要说明。
/// </summary>
public class Packet
...{
private byte[] m_Raw;
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;
private 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, DateTime.Now);
// }
public Packet(byte[] raw, DateTime time)
...{
if (raw==null)
...{
throw new ArgumentNullException();
}
if (raw.Length<20)
...{
throw new ArgumentException();
}
this.m_Raw = raw;
this.m_Time = time;
this.m_HeaderLength = (raw[0] & 0xF) * 4;
if ((raw[0] & 0xF) < 5) ...{throw new ArgumentException();}
this.m_Precedence = (Precedence)((raw[1] & 0xE0) >> 5);
this.m_Delay = (Delay)((raw[1] & 0x10) >> 4);
this.m_Throughput = (Throughput)((raw[1] & 0x8) >> 3);
this.m_Reliability = (Reliability)((raw[1] & 0x4) >> 2);
this.m_TotalLength = raw[2] * 256 + raw[3];
if ( ! (this.m_TotalLength == raw.Length))
...{
throw new ArgumentException();
} // invalid size of packet;
this.m_Identification = raw[4] * 256 + raw[5];
this.m_TimeToLive = raw[8];
m_Protocol = (Protocol)raw[9];
m_Checksum = new byte[2];
m_Checksum[0] = raw[11];
m_Checksum[1] = raw[10];
try
...{
m_SourceAddress = GetIPAddress(raw, 12);
m_DestinationAddress = GetIPAddress(raw, 16);
}
catch (Exception e)
...{
throw;
}
if (m_Protocol == Protocol.Tcp || m_Protocol == Protocol.Udp)
...{
m_SourcePort = raw[m_HeaderLength] * 256 +
raw[m_HeaderLength + 1];
m_DestinationPort = raw[m_HeaderLength + 2] *
256 + raw[m_HeaderLength + 3];
}
else
...{
m_SourcePort = -1;
m_DestinationPort = -1;
}
}
public string GetIPAddress(byte[] bArray, int nStart)
...{
byte[] tmp = new byte[4];
if (bArray.Length > nStart + 2)
...{
tmp[0] = bArray[nStart];
tmp[1] = bArray[nStart + 1];
tmp[2] = bArray[nStart + 2];
tmp[3] = bArray[nStart + 3];
}
return tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + tmp[3];
}
public int TotalLength
...{
get ...{ return m_TotalLength; }
}
public DateTime Time
...{
get
...{
return this.m_Time;
}
}
public Protocol Protocol
...{
get ...{ return this.m_Protocol; }
}
public string SourceAddress
...{
get ...{ return this.m_SourceAddress; }
}
public string Source
...{
get
...{
if ( m_SourcePort != -1 )
...{
return SourceAddress.ToString() + ":" +
m_SourcePort.ToString();
}
else
...{
return SourceAddress.ToString();
}
}
}
public string Destination
...{
get
...{
if (this.m_DestinationPort != -1)
...{
return DestinationAddress.ToString() + ":" +
m_DestinationPort.ToString();
}
else
...{
return DestinationAddress.ToString();
}
}
}
public string DestinationAddress
...{
get
...{
return m_DestinationAddress;
}
}
}
}
在主程序里
private Monitor[] m_PacketMonitors;
private ArrayList m_Packets;
private System.Windows.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; i<hosts.Length; i++)
...{
}
m_PacketMonitors = new Monitor[1];
m_Packets = new ArrayList();
m_PacketMonitors[0] = new Monitor(hosts[0]);
// 添加代理,每次有新的packet到时都出发下面哪个动作
m_PacketMonitors[0].NewPacket+=
new Monitor.NewPacketEventHandler(this.OnNewPacket);
m_PacketMonitors[0].Start();
}
// 这个方法用于把packet显示到一个地方
private void OnNewPacket(Monitor m, Packet p)
...{
m_Packets.Add(p);
m_PacketsSize += p.TotalLength;
try
...{
txtLog.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);
}
用c#监控网络流量的更多相关文章
- 运用Ntop监控网络流量(视频Demo)
运用Ntop监控网络流量 ____网络流量反映了网络的运行状态,是判别网络运行是否正常的关键数据,在实际的网络中,如果对网络流量控制得不好或发生网络拥塞,将会导致网络吞吐量下降.网络性能降低.通过流量 ...
- ubuntu下使用nethogs监控网络流量
NetHogs是一款小巧免费的开源命令行工具,用来按进程或程序实时统计网络带宽使用率. 对于使用类似于“repo tool”.“depot_tools”等工具checkout源码时非常有用,可以查看当 ...
- Ntop监控网络流量
运用Ntop监控网络流量 ____ 网络流量反映了网络的运行状态,是判别网络运行是否正常的关键数据,在实际的网络中,如果对网络流量控制得不好或发生网络拥塞,将会导致网络吞吐量下降. 网络性能降低.通过 ...
- mrtg监控网络流量简单配置
Mrtg服务器搭建(监控网络流量) [日期:2012-07-03] 来源:Linux社区 作者:split_two [字体:大 中 小] [实验环境] 监控机:Red Hat linux 5.3 ...
- linux下脚本监控网络流量
linux下脚本监控网络流量 学习了:https://blog.csdn.net/chenghuikai/article/details/48437479 学习了:http://www.jb51.ne ...
- Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量
Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...
- 监控网络流量iftop和nethogs安装
服务器环境是centos7,centos下通常使用iftop,或者nethogs来进行网络流量监控.这2个工具都需要先安装epel,因为这个库通常操作系统是不自带的.那么就先安装epel,使用的命令是 ...
- centos8平台基于iftop监控网络流量
一,iftop的作用: 基于ip统计外部机器与本机之间的网络流量, 可以方便的查看各客户端是否有非正常的到本机的访问 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnbl ...
- C#实现监控网络流量
本文转载自:http://blog.csdn.net/zhanjianshinian/article/details/8177851 public partial class NetJiankongF ...
- Linux图形化监控网络流量:speedometer查看流量
Speedometer是一个带宽控制台和对数带宽显示的文件下载进度监控,以及一个简单的命令行界面.其目的是测量和显示网络连接或数据存储在文件中的数据率. Speedometer 2.8该版本增加了一个 ...
随机推荐
- Nginx location wildcard
Module ngx_http_core_modulehttps://nginx.org/en/docs/http/ngx_http_core_module.html#location locatio ...
- [转]Java 之 Serializable 序列化和反序列化的概念,作用的通俗易懂的解释
原文地址:https://blog.csdn.net/qq_27093465/article/details/78544505 遇到这个 Java Serializable 序列化这个接口,我们可能会 ...
- 30段极简Python代码
Python 是机器学习最广泛采用的编程语言,它最重要的优势在于编程的易用性.如果读者对基本的 Python 语法已经有一些了解,那么这篇文章可能会给你一些启发.作者简单概览了 30 段代码,它们都是 ...
- WebGL学习笔记(一):理解基本概念和渲染管线
WebGL 是以 OpenGL ES 2.0 为基础的 3D 编程应用接口. 渲染管线(图形流水线) 渲染管线是指将数据从3D场景转换成2D图像,最终在屏幕上显示出来的总过程.它分为几个阶段:应用阶段 ...
- QT中添加图片资源
1.在ui设计界面中添加label,用于显示图片 2.添加QT资源文件 往项目中添加新文件,选择QT分类中的资源文件,名称为"myImage",其他选项默认. 3.添加资源 在项目 ...
- LODOP表格table简短问答及相关博文
LODOP打印表格超文本输出表格:ADD_PRINT_HTML.ADD_PRINT_HTM.ADD_PRINT_TABLE.ADD_PRINT_TBURL打印表格带页头页尾 参考样例15 http:/ ...
- rConfig v3.9.2 授权认证与未授权RCE (CVE-2019-16663) 、(CVE-2019-16662)
rConfig v3.9.2 authenticated and unauthenticated RCE (CVE-2019-16663) and (CVE-2019-16662) 原文:https: ...
- 画图前端:mermaid。时序图/类图/甘特图/流程图/状态图/饼图。类似工具:Typora
文档 https://mermaidjs.github.io/#/ cdn https://www.bootcdn.cn/mermaid/ 在线编辑 https://mermaidjs.github. ...
- Layer文件上传同时传递表单数据
(1)index.html <!DOCTYPE html> <html> <head> <title>TODO supply a title</t ...
- 喜马拉雅 FM 已购付费音频下载
如何下载在喜马拉雅 FM 中已购买的付费音频.之前想分享自己购买的付费音频给朋友听,碍于喜马拉雅 FM 的音频不能直接导出,所以准备自己搞个下载的小软件. 仅可下载已购买的付费音频.当然,如果你是会员 ...