首页 > 编程 > .NET > 正文

VB.NET编写的TCP异步通讯类(目前测试中)

2024-07-10 13:00:47
字体:
来源:转载
供稿:网友

这个类还没有完全ok,但基本的功能已经完成,异常还有待改进,欢迎批评。

imports system.threading
imports system.net
imports system.net.sockets
imports system.text
imports system.componentmodel

<defaultevent("dataarrival")> public class mytcpclient
private m_sckclient as socket
private ipepremote as ipendpoint
private m_conndone as new manualresetevent(false)
private m_senddone as new manualresetevent(false)
private m_receivedone as new manualresetevent(false)

public event dataarrival as dataarrivalhandler
public event connectioncomplete as eventhandler
public event disconnect as eventhandler

public readonly property connected() as boolean
get
return me.m_sckclient.connected
end get
end property
public readonly property remoteipendpoint() as ipendpoint
get
return me.ipepremote
end get
end property

public sub new(byval remoteport as integer, byval remoteip as string, byval localport as integer)
try
me.setsocket(remoteport, remoteip, localport)
catch ex as exception
throw new exception("new--" & ex.tostring)
end try
end sub

public sub setsocket(byval remoteport as integer, byval remoteip as string, byval localport as integer)

me.m_sckclient = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp)
ipepremote = new ipendpoint(ipaddress.parse(remoteip), remoteport)
me.m_sckclient.blocking = false

me.m_sckclient.setsocketoption(socketoptionlevel.tcp, socketoptionname.nodelay, 0)
me.m_sckclient.beginconnect(me.ipepremote, new system.asynccallback(addressof cbconn), m_sckclient)

if me.m_conndone.waitone(3000, false) = false then
me.m_sckclient = nothing
throw new exception("setsocket--noconnection,目标主机没有响应")
end if

end sub

public sub senddata(byval strsend as string)
if not me.m_sckclient is nothing then
if me.m_sckclient.connected then
dim strready as string = chr(1) & strsend & computechecksum(strsend) & chr(4)
me.m_sckclient.beginsend(system.text.encoding.default.getbytes(strready), 0, strready.length, socketflags.none, new system.asynccallback(addressof me.cbsend), me.m_sckclient)
me.m_senddone.waitone()
else
throw new socketexception("senddata--,连接还没有打开")
end if
else
throw new exception("senddata--,未将对象引用设置到对象的实例")
end if
end sub

public sub close()
if not me.m_sckclient is nothing then
if me.m_sckclient.connected then
me.m_sckclient.shutdown(socketshutdown.both)
me.m_sckclient.close()
else
throw new exception("close--,连接还没有打开")
end if
else
throw new exception("close--,未将对象引用设置到对象的实例")
end if
end sub

public sub dispose()
if not me.m_sckclient is nothing then
if me.m_sckclient.connected then
me.m_sckclient.shutdown(socketshutdown.both)
me.m_sckclient.close()
end if
me.m_sckclient = nothing
end if
if not me.ipepremote is nothing then
me.ipepremote = nothing
end if
end sub

#region "回调函数"
private sub cbconn(byval ar as system.iasyncresult)
dim objsocket as socket = ctype(ar.asyncstate, socket)
if objsocket.connected then
try
objsocket.endconnect(ar)
me.m_conndone.set()
dim state as new sckstructure
state.worksocket = me.m_sckclient
me.m_sckclient.beginreceivefrom(state.buffer, 0, state.buffersize, socketflags.none, me.ipepremote, addressof me.cbreceive, state)
me.m_receivedone.waitone()
catch ex as exception
throw new exception("cbconn--hasconnected" & ex.tostring)
end try
raiseevent connectioncomplete(me, new eventargs)
else
throw new exception("cbconn--noconnection,目标主机没有响应")
end if
end sub

private sub cbsend(byval ar as system.iasyncresult)
try
me.m_sckclient.endsend(ar)
me.m_senddone.set()
catch ex as exception
throw new exception("cbsend--" & ex.tostring)
end try
end sub

private sub cbreceive(byval ar as system.iasyncresult)
dim bytesread as integer
try
bytesread = me.m_sckclient.endreceive(ar)
me.m_receivedone.set()
catch ex as socketexception
if ex.errorcode = 10054 then
raiseevent disconnect(me, new eventargs)
else
throw new exception("cbreceive--" & ex.tostring)
end if
catch ex as exception
throw new exception("cbreceive--" & ex.tostring)
end try
try
dim obj1 as new sckstructure
obj1.worksocket = me.m_sckclient
me.m_sckclient.beginreceive(obj1.buffer, 0, obj1.buffersize - 1, socketflags.none, addressof me.cbreceive, obj1)
me.m_receivedone.waitone()
dim obj as sckstructure = ctype(ar.asyncstate, sckstructure)
dim strreceive as string = encoding.default.getstring(obj.buffer, 0, bytesread)
if cleanstring(strreceive) <> "" then
raiseevent dataarrival(strreceive)
end if
catch ex as socketexception
if ex.errorcode = 10054 then
raiseevent disconnect(me, new eventargs)
else
throw new exception("cbreceive--" & ex.tostring)
end if
catch ex as exception
throw new exception("cbreceive--" & ex.tostring)
end try

end sub
#end region
end class

friend class sckstructure
public worksocket as socket = nothing
public const buffersize as integer = 1024
public buffer(buffersize) as byte
end class

public delegate sub dataarrivalhandler(byval strreceive as string)



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表