转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.net进阶:VB.net下的Sniffer         ★★★★

VB.net进阶:VB.net下的Sniffer

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1456 更新时间:2009/4/23 19:00:53

原来的C#代码在这里:
http://www.techblog.idv.tw/Blog/zion/archive/2004/09/14/148.aspx

改写后的代码分成两部分:Receiver,用来侦听;PacketInfo,对数据包进行简单的解析。
数据包的内容是通过Receiver的DataReceived事件返回来的。
每个函数都不长,容易看懂,注释我就……咳咳。




Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class Receiver

    Dim buffer As Byte()
    Dim mvarBufferLength As Integer = 4096
    Dim sck As Socket
    Dim thrReceive As Thread
    Dim mvarStopOne As Boolean = False

    Public Event DataReceived(ByVal data As Byte(), ByVal Length As Integer)

    Sub New()
        ReDim buffer(mvarBufferLength)

        sck = New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
        sck.Blocking = False
        sck.Bind(New IPEndPoint(Dns.GetHostByName(Dns.GetHostName).AddressList(0), 0))

        If Not SetSockoption() Then Throw New Exception("Unable to setup socket options")
    End Sub

    Public Property BufferLength() As Integer
        Get
            Return mvarBufferLength
        End Get
        Set(ByVal Value As Integer)
            If Not thrReceive Is Nothing Then
                If thrReceive.ThreadState = ThreadState.Running Then Throw New Exception("Receiving thread is running. Call StopReceive() first.")
            End If
            ReDim buffer(Value)
            mvarBufferLength = Value
        End Set
    End Property

    Public Property StopEveryOnePackage() As Boolean    ''''指定是否接受一个数据包后就退出。用于测试。
        Get
            Return mvarStopOne
        End Get
        Set(ByVal Value As Boolean)
            mvarStopOne = Value
        End Set
    End Property

    Public Sub StartReceive()
        StopReceive()
        thrReceive = New Thread(AddressOf subReceive)
        thrReceive.Start()
    End Sub

    Public Sub StopReceive()
        Try
            thrReceive.Abort()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub subReceive()
        Dim i As Integer, ar As IAsyncResult
        Dim b As Byte()
        While True
            ar = sck.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, Nothing, Me)
            i = sck.EndReceive(ar)
            ReDim b(i)
            Array.Copy(buffer, 0, b, 0, i)
            RaiseEvent DataReceived(b, i)
            Thread.CurrentThread.Sleep(10)
            If Me.StopEveryOnePackage Then Exit While
        End While
    End Sub

    Private Function SetSockoption() As Boolean
        Try
            sck.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1)
            Dim IN_() As Byte = {1, 0, 0, 0}
            Dim OUT_(4) As Byte
            Dim SIO_RCVALL As Long = &H98000001
            sck.IOControl(SIO_RCVALL, IN_, OUT_)
            If (BitConverter.ToInt32(OUT_, 0) <> 0) Then Return False
        Catch ex As SocketException
            Return False
        End Try
        Return True
    End Function
End Class



------------------------------------------------------------------------------------------




Imports System.Net

Public Class PacketInfo
    Dim data As Byte()

    Sub New(ByVal PacketData As Byte())
        data = PacketData
    End Sub

    Public ReadOnly Property Protocal() As System.Net.Sockets.ProtocolType
        Get
            Select Case GetProtocal()
                Case 17
                    Return Net.Sockets.ProtocolType.Udp
                Case 6
                    Return Net.Sockets.ProtocolType.Tcp
                Case 1
                    Return Net.Sockets.ProtocolType.Icmp
                Case Else
                    Return Net.Sockets.ProtocolType.Unknown
            End Select
        End Get
    End Property

    Public ReadOnly Property Sender() As IPEndPoint
        Get
            If Me.Protocal = Sockets.ProtocolType.Unknown Then Return Nothing
            Return GetSenderIPEndPoint()
        End Get
    End Property

    Public ReadOnly Property Receiver() As IPEndPoint
        Get
            If Me.Protocal = Sockets.ProtocolType.Unknown Then Return Nothing
            Return GetreceiverIPEndPoint()
        End Get
    End Property

    Public ReadOnly Property PacketData() As Byte()
        Get
            If Me.Protocal = Sockets.ProtocolType.Unknown Then Return Nothing
            Return GetData()
        End Get
    End Property

 

    Private Function GetProtocal() As Integer
        Return data(9)
 &nb

[1] [2]  下一页


[Delphi程序]Xray@NET,业余写的SNIFFER  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台