用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);

}

时间: 2024-08-29 15:53:48

用c#监控网络流量的相关文章

Centos 6安装Cacti监控网络流量

Centos 6安装Cacti监控网络流量 1.操作系统的安装,这个就不用写教程了吧(略)我个人用的是CentOS6.5最小化安装装好系统以后关闭防火墙和selinux#service iptables stop     /*关闭防火墙服务*/#chkconfig iptables off    /*永久性关闭*/ #vim /etc/sysconfig/selinux   /*把SELINUX=enforcing改为SELINUX=disabled*/ 2.安装cacti依赖的软件 #yum

搭建Ntopng监控网络流量情况

ntopng是高速的基于Web的流量分析与集流工具.ntopng是ntop的新一代版本,官方原先版本的ntop已经不再更新.用户可以使用网页浏览器浏览查看网络中的流量信息,从而分析网络瓶颈. 1. 环境描述: 本文使用操作系统CentOS6.4-64bit,采用源码(Source code)的方式安装,本文使用ntop-1.1版本,ntopng下载地址:ntopng下载 http://www.ntop.org/get-started/download/. 2.安装依赖 rpm -ivh epel

ubuntu下使用nethogs监控网络流量

NetHogs是一款小巧免费的开源命令行工具,用来按进程或程序实时统计网络带宽使用率. 对于使用类似于"repo tool"."depot_tools"等工具checkout源码时非常有用,可以查看当前的下载速度信息,让你不白瞎等待. Ubuntu系统下安装 sudo apt-get install nethogs 使用方法   nethogs [-d] [-h] [-p] [-t] [-V] [device(s)] The -d switch delay for

Zabbix监控网络流量

一般来说,云服务器都会自带云监控,而且"流量监控"也是常备的监控项. 对于内网网卡来说,有一个比较不错的查看工具叫nethogs,可以直接yum,#yum install nethogs,而查看内网网卡流量的方法也很简单,#nethogs eth0.效果如图: 言归正传,我们现在要搞的是用zabbix去监控网络的流量,由于金山云的linux控制台只能显示eth0,而没有eth1(外网网卡),所以我们这次就用监控eth0来做例子. zabbix自带的监控网络流量的key是:net.if.

Ntop监控网络流量

运用Ntop监控网络流量 ____ 网络流量反映了网络的运行状态,是判别网络运行是否正常的关键数据,在实际的网络中,如果对网络流量控制得不好或发生网络拥塞,将会导致网络吞吐量下降. 网络性能降低.通过流量测量不仅能反映网络设备(如路由器.交换机等)的工作是否正常,而且能反映出整个网络运行的资源瓶颈,这样管理人员就可以根据网络 的运行状态及时采取故障补救措施和进行相关的业务部署来提高网络的性能.对网络进行流量监测分析,可以建立网络流量基准,通过连接会话数的跟踪.源/目的 地址对分析.TCP流的分析

mrtg监控网络流量简单配置

Mrtg服务器搭建(监控网络流量) [日期:2012-07-03] 来源:Linux社区  作者:split_two [字体:大 中 小] [实验环境] 监控机:Red Hat linux 5.3  IP:10.10.10.2/24 监控机的操作窗口为绿色字体 被监控机:Red Hat linux 5.3  IP:10.10.10.10/24 操作为白色字体 [实验目的] 监控网络流量 [实验步骤] 1.监控机上需要安装3个软件包,net-snmp(安装一个网管协议).mrtg(此次监控软件).

用iftop监控网络流量

iftop是很有用的工具,下面的命令监控了我的笔记本的无线网卡 iftop -i wlan0 比如我现在播放乐视一个视频,iftop显示的信息: 基本说明: 1. 屏幕主要部分都是表示两个机器之间的数据传送,有箭头表示方向,右边三个数值分别是过去2秒,10秒和40秒的平均流量. 2  左下角的TX 表示发出的数据,RX表示收到的数据, cum表示总流量, peak表示对应的峰值, Total就不用解释了. 用iftop监控网络流量

解决zabbix用snmp监控网络流量不准的问题

公司新上了一个新的数据中心,需要用zabbix监控华三交换机的网络流量. 配好snmp协议之后,正常都能识别,但慢慢的发现一个问题,电信的接口经常出现少数据的情况,但联通和铁通都没有什么问题. zabbix绘的图断断续续的,有时更神奇的是,流量突然下降,下降的还很离谱,从500多Mbps一下掉到40多Mbps,一度以为是交换机返回的数据有问题了. 后来在zabbix机器上抓包,发现数据包没有丢,数据也没有什么异常,不过抓的包里面,有个counter32引起注意. 然后仔细阅读snmp的文档,发现

监控网络流量iftop和nethogs安装

服务器环境是centos7,centos下通常使用iftop,或者nethogs来进行网络流量监控.这2个工具都需要先安装epel,因为这个库通常操作系统是不自带的.那么就先安装epel,使用的命令是: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 在epel安装成功之后,接下来我们就可以用yum安装iftop和nethogs.

nagios监控网络流量

系统版本 [[email protected] libexec]# cat /etc/redhat-release CentOS release 5.6 (Final) 1 安装和配置snmp 1)yum install net-snmp-utils net-snmp net-snmp-libs -y 2)vi /etc/snmp/snmpd.conf 把下面两行的#号去掉 #view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc #view