动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ C#应用 ]的信息

本月文章推荐
.用http代理下载sourceforge的cvs.
.深入解析C#编程中的事件.
.在C#中编写多线程应用程序,简单.
.使用C#编写一个计时器.
.C#源码读取excel数据到程序中-SQ.
.C#分析数据库结构,使用XSL模板自.
.C# Operate Excel File .
.C#中Dispose和Close的区别.
.用Visual C#调用Windows API函数.
.如何用C#把Doc文档转换成rtf格式.
.教你如何快速捕获.NET代码中隐藏.
.在C#应用程序与DLL交互中使用消息.
.用C#对DBF数据库的操作.
.C#模拟MSN窗体抖动.
.C#投票作弊程序制作思路.
.如何在无刷新页面的情况下实现客.
.信息反馈-邮件(数据库是XML) .
.用C#动态创建Access数据库.
.C#代码与JavaScript函数的相互调.
.在C#里使用using操作符.

用c#监控网络流量

发表日期:2006-4-7 |


        以下的代码的原作是opensource的一个叫sniffer.net的,用vb.net写的,这里只是简单的用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#中得到程序当前工作目录和执行目录的一些方法 人气:9781
下一篇:c# arraylist functions 人气:8075
浏览全部c#的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-12-2 OpenPNE中文 v2.12.5 for win 中
2008-12-2 谷秋精品课程软件课程版 v2.3
2008-12-2 晴天电影系统(带一键迅雷/自定义
2008-12-2 QQip138闪字程序
2008-12-2 SmartWeb企业智能建站系统 v1.0.2
2008-12-2 梦想不死个人主页 v2009
2008-12-2 开良ASP小偷程序生成器 v1.1
2008-12-2 toolxp.cnalexa世界排名查询 php
2008-12-2 腾讯留言板 v1.3
2008-11-29 Tencent Traveler 4.4
2008-11-29 龙卷风网络收音机 v3.0.0.0
2008-11-29 Intel Chipset Software Install
2008-11-29 TweakVI 1.0 Build 1100
2008-11-29 Opera 9.62 Build 10469
2008-11-29 MPlayer WW编译版 SVN-r28044(20
2008-11-29 NetTools网络工具v1.0.0破解版
2008-11-29 3DGallery三维体验1.1破解版
2008-11-29 SecretBook保密本v1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | OPEN参数生成器 | 弹出式窗口代码产生器 | 密码登录生成器 | 在线按钮生成器 | Meta标签生成器 | 多色彩特效字代码生成器 | 网页代码调试器 | 在线FTP登陆 | Flash取色器 | 配色代码对照表 | 配色辞典 | CSS生成器 | 广告代码 | 框架网页代码生成器 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | 在线调色板 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 元素周期表 | 健康查询 | 世界时间 | 万年历 | 二十四节气 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2009 www.knowsky.com All rights reserved | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 中国語教室 ホームページ作成