国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .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)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐闻县| 兖州市| 留坝县| 娄烦县| 潞西市| 昂仁县| 兴海县| 海兴县| 盘锦市| 四子王旗| 山阴县| 安宁市| 玛纳斯县| 赤水市| 昆明市| 郯城县| 周宁县| 平度市| 南召县| 珲春市| 铁岭县| 扎囊县| 桐城市| 通道| 普安县| 桦川县| 旺苍县| 屏东县| 阿拉善盟| 泰顺县| 保靖县| 得荣县| 武安市| 平阳县| 榆社县| 邵阳市| 渑池县| 湖北省| 五常市| 望奎县| 来安县|