改写后的代码分成两部分: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)
end function
private function getsenderipendpoint() as ipendpoint
return new ipendpoint(getsenderaddress, getsenderport)
end function
private function getreceiveripendpoint() as ipendpoint
return new ipendpoint(getreceiveraddress, getreceiverport)
end function
private function getsenderaddress() as ipaddress
return getaddress(12)
end function
private function getsenderport() as integer
return getport(20)
end function
private function getreceiveraddress() as ipaddress
return getaddress(16)
end function
private function getreceiverport() as integer
return getport(21)
end function
private function getaddress(byval startindex as integer) as ipaddress
dim b(3) as byte
array.copy(data, startindex, b, 0, 4)
return ipaddress.parse(string.format("{0}.{1}.{2}.{3}", b(0), b(1), b(2), b(3)))
end function
private function getport(byval startindex as integer) as integer
return data(startindex) * 256 + data(startindex + 1)
end function
private function getdata() as byte()
dim b as byte()
dim headerlength as integer
select case me.protocal
case sockets.protocoltype.tcp
headerlength = 40
case sockets.protocoltype.udp
headerlength = 28
end select
redim b(data.length - headerlength)
array.copy(data, headerlength, b, 0, data.length - headerlength)
return b
end function
end class