微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一个基于VB.net的异步Socket网络TCP通信可防止任意一端意外终止TCP连接的类,

之前,因为要做一个TCP通信的项目,有研究一下Socket类,但是为了快速完成任务,还是在网上找了一些源码来调试测试,发现很多源码都无法触发TCP连接的任意一端

的突然意外中断连接的事件,于是本人基于他人的源码基础上进行了修改,可以触发这一事件,可使TCP连接的另一端触发对方已经终止TCP连接事件。

以下,奉上本人修改后的源码类:

1)TCP 服务器TCP 侦听类。

  1. ImportsSystem.Net
  2. ImportsSystem.Net.sockets
  3. ImportsSystem.Threading
  4. ImportsSystem.Text
  5. '**********************************************************************************************************
  6. ''''''类名:Tcpserver
  7. ''''''说明:监听主线程,用于监听客户端联接,并记录客户端联接,接收和发送数据
  8. ''''''与客户端的联接采用TCP联接
  9. '**********************************************************************************************************
  10. '''<summary>
  11. '''侦听客户端联接
  12. '''</summary>
  13. PublicClassTcpserver
  14. #Region"私有成员"
  15. Private_LocationListenSocketAsSocket'本地侦听服务
  16. Private_ListenPortAsString'服务器侦听客户端联接的端口
  17. Private_MaxClientAsInteger'最大客户端连接数
  18. Private_ClientsAsNewSortedList'客户端队列
  19. Private_ListenThreadAsThread=nothing'侦听线程
  20. Private_ServerStartAsBoolean=False'服务器是否已经启动
  21. Private_RecvMaxAsInteger'接收缓冲区大小
  22. #EndRegion
  23. #Region"事件"
  24. '''<summary>
  25. '''客户端联接事件
  26. '''</summary>
  27. '''<paramname="IP">客户端联接IP</param>
  28. '''<paramname="Port">客户端联接端口号</param>
  29. '''<remarks></remarks>
  30. PublicEventClientConnected(ByValIPAsString,ByValPortAsString)
  31. '''<summary>
  32. '''客户端断开事件
  33. '''</summary>
  34. '''<paramname="IP">客户端联接IP</param>
  35. '''<paramname="Port">客户端联接端口号</param>
  36. '''<remarks></remarks>
  37. PublicEventClientClose(ByValIPAsString,ByValPortAsString)
  38. '''<summary>
  39. '''接收到客户端的数据
  40. '''</summary>
  41. '''<paramname="value">数据</param>
  42. '''<paramname="IPAddress">数据来源IP</param>
  43. '''<paramname="Port">数据来源端口</param>
  44. '''<remarks></remarks>
  45. PublicEventDataArrived(ByValvalueAsByte(),ByValLenAsInteger,ByValIPAddressAsString,ByValPortAsString)
  46. '''<summary>
  47. '''异常数据
  48. '''</summary>
  49. '''<paramname="ex"></param>
  50. '''<remarks></remarks>
  51. PublicEventException(ByValexAsException)
  52. #EndRegion
  53. #Region"属性"
  54. '''<summary>
  55. '''侦听服务是否已经启动
  56. '''</summary>
  57. '''<value></value>
  58. '''<returns></returns>
  59. '''<remarks></remarks>
  60. PublicReadOnlyPropertyIsServerStart()AsBoolean
  61. Get
  62. Return_ServerStart
  63. EndGet
  64. EndProperty
  65. #EndRegion
  66. #Region"方法"
  67. '''<summary>
  68. '''实例 Tcpserver
  69. '''</summary>
  70. '''<paramname="Port">侦听客户端联接的端口号</param>
  71. '''<paramname="MaxClient">最大可以联接的客户端数量</param>
  72. '''<paramname="RecvMax">接收缓冲区大小</param>
  73. '''<paramname="RecvSleep">接收线程睡眠时间</param>
  74. '''<remarks></remarks>
  75. SubNew(ByValPortAsString,ByValMaxClientAsInteger,ByValRecvMaxAsInteger,ByValRecvSleepAsInteger)
  76. Try
  77. DimstrHostNameAsString=Dns.GetHostName()
  78. _ListenPort=Port
  79. _MaxClient=MaxClient
  80. _RecvMax=RecvMax
  81. DimstrServerHostAsNewIPEndPoint(IPAddress.Any,Int32.Parse(_ListenPort))
  82. '建立TCP侦听
  83. _LocationListenSocket=NewSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)
  84. _LocationListenSocket.Bind(strServerHost)
  85. _LocationListenSocket.Listen(_MaxClient)
  86. _LocationListenSocket.SetSocketoption(SocketoptionLevel.Tcp,SocketoptionName.AcceptConnection,1)
  87. CatchexAsException
  88. RaiseEventException(ex)
  89. EndTry
  90. EndSub
  91. '''<summary>
  92. '''开始侦听服务
  93. '''</summary>
  94. '''<remarks></remarks>
  95. PublicSubStartServer()
  96. _ServerStart=True
  97. Try
  98. _ListenThread=NewThread(NewThreadStart(AddressOfListenClient))
  99. _ListenThread.Name="监听客户端主线程"
  100. _ListenThread.Start()
  101. CatchexAsException
  102. If(Not_LocationListenSocketIsnothing)Then
  103. If_LocationListenSocket.ConnectedThen
  104. _LocationListenSocket.Close()
  105. EndIf
  106. EndIf
  107. RaiseEventException(ex)
  108. EndTry
  109. EndSub
  110. '''<summary>
  111. '''关闭侦听
  112. '''</summary>
  113. '''<remarks></remarks>
  114. PublicSubClose()
  115. Try
  116. _ServerStart=False
  117. 'CloseAllClient()
  118. Thread.Sleep(5)
  119. _ListenThread.Abort()
  120. _LocationListenSocket.Close()
  121. _ListenThread=nothing
  122. CatchexAsException
  123. RaiseEventException(ex)
  124. EndTry
  125. EndSub
  126. '''<summary>
  127. '''客户端侦听线程
  128. '''</summary>
  129. '''<remarks></remarks>
  130. PrivateSubListenClient()
  131. DimsKeyAsString
  132. While(_ServerStart)
  133. Try
  134. IfNot_LocationListenSocketIsnothingThen
  135. DimclientSocketAsSystem.Net.sockets.socket
  136. clientSocket=NewSocket(AddressFamily.InterNetwork,ProtocolType.Tcp)
  137. clientSocket=_LocationListenSocket.Accept()
  138. IfNotclientSocketIsnothingThen
  139. DimclientInfoTAsIPEndPoint=CType(clientSocket.RemoteEndPoint,IPEndPoint)
  140. sKey=clientInfoT.Address.ToString&"\&"&clientInfoT.Port.ToString
  141. _Clients.Add(sKey,clientSocket)
  142. RaiseEventClientConnected(clientInfoT.Address.ToString,clientInfoT.Port.ToString)'举起有客户端联接的事件
  143. '启动客户端接收主线程,开始侦听并接收客户端上传的数据
  144. DimlbAsNewClientCommunication(_LocationListenSocket,clientSocket,Me)
  145. AddHandlerlb.Exception,AddressOfWriteErrorEvent_ClientCommunication
  146. DimthrClientAsNewThread(NewThreadStart(AddressOflb.serverThreadProc))
  147. thrClient.Name="客户端接收线程,客户端"&clientInfoT.Address.ToString&":"&clientInfoT.Port.ToString
  148. thrClient.Start()
  149. EndIf
  150. EndIf
  151. CatchexAsException
  152. RaiseEventException(ex)
  153. EndTry
  154. EndWhile
  155. EndSub
  156. PrivateSubWriteErrorEvent_ClientCommunication(ByValexAsException)
  157. RaiseEventException(ex)
  158. EndSub
  159. PublicSubCloseClient(ByValIPAsString,ByValPortAsString)
  160. GetClientSocket(IP,Port).Close()
  161. GetClientClose(IP,Port)
  162. EndSub
  163. 'PublicSubAlertNoticeClientAll(ByValDepartmentNameAsstring,ByValLineNameAsstring,ByValErrorCodeAsInteger)
  164. ''#DepartmentName,LineName,AlertCodeValue.
  165. ''''DimmStrAsstring
  166. ''''mStr="#"&DepartmentName&","&LineName&","&ErrorCode
  167. ''''DimsendByte()AsByte=System.Text.UTF8Encoding.Default.GetBytes(mStr)
  168. ''''ForEachscAsSystem.Net.sockets.socketIn_ClientComputers.Values
  169. ''''sc.Send(SendByte,SendByte.Length(),SocketFlags.None)
  170. ''''Next
  171. 'EndSub
  172. PublicSubCloseAllClient()
  173. ForEachscAsSystem.Net.sockets.socketIn_Clients.Values
  174. '断开所有工作站的Socket连接。
  175. DimclientInfoTAsIPEndPoint=CType(sc.RemoteEndPoint,IPEndPoint)
  176. CloseClient(clientInfoT.Address.ToString,clientInfoT.Port.ToString)
  177. Next
  178. EndSub
  179. #Region"接收客户端的数据"
  180. '''<summary>
  181. '''接收到客户端的数据-字节数组
  182. '''</summary>
  183. '''<paramname="value">数据内容</param>
  184. '''<paramname="Len">字节长度</param>
  185. '''<paramname="IPAddress">发送该数据的IP地址</param>
  186. '''<paramname="Port">发送该数据的端口号</param>
  187. '''<remarks></remarks>
  188. PrivateSubGetData_Byte(ByValvalueAsByte(),ByValPortAsString)
  189. Try
  190. RaiseEventDataArrived(value,Len,IPAddress,Port)
  191. 'CatchexxAsSockets.socketException
  192. 'CloseClient(IPAddress,Port)
  193. CatchexAsException
  194. RaiseEventException(ex)
  195. EndTry
  196. EndSub
  197. '''<summary>
  198. '''得到客户端断开或失去客户端联连事件
  199. '''</summary>
  200. '''<paramname="IP">客户端联接IP</param>
  201. '''<paramname="Port">客户端联接端口号</param>
  202. '''<remarks></remarks>
  203. PrivateSubGetClientClose(ByValIPAsString,ByValPortAsString)
  204. Try
  205. If_Clients.ContainsKey(IP&"\&"&Port)Then
  206. SyncLock_Clients.SyncRoot
  207. '_Clients.Item(IP&"\&"&Port)
  208. _Clients.Remove(IP&"\&"&Port)
  209. EndSyncLock
  210. EndIf
  211. RaiseEventClientClose(IP,Port)
  212. CatchexAsException
  213. RaiseEventException(ex)
  214. EndTry
  215. EndSub
  216. #EndRegion
  217. #Region"向客户端发送数据"
  218. '''<summary>
  219. '''向客户端发送信息
  220. '''</summary>
  221. '''<paramname="value">发送的内容</param>
  222. '''<paramname="IPAddress">IP地址</param>
  223. '''<paramname="Port">端口号</param>
  224. '''<returns>Boolean</returns>
  225. '''<remarks></remarks>
  226. PublicFunctionSendData(ByValvalueAsByte(),ByValPortAsString)AsBoolean
  227. Try
  228. DimclientSocketAsSystem.Net.sockets.socket
  229. clientSocket=_Clients.Item(IPAddress&"\&"&Port)
  230. clientSocket.Send(value,value.Length,SocketFlags.None)
  231. ReturnTrue
  232. CatchexAsException
  233. RaiseEventException(ex)
  234. ReturnFalse
  235. EndTry
  236. EndFunction
  237. PublicFunctionSendFile(ByValvalueAsString,ByValPortAsString)AsBoolean
  238. Try
  239. DimclientSocketAsSystem.Net.sockets.socket
  240. clientSocket=_Clients.Item(IPAddress&"\&"&Port)
  241. clientSocket.SendFile(value)
  242. ReturnTrue
  243. CatchexAsException
  244. RaiseEventException(ex)
  245. ReturnFalse
  246. EndTry
  247. EndFunction
  248. PublicFunctionSendDataToAllClient(ByValvalueAsByte())AsBoolean
  249. Try
  250. ForEachclientSocketAsSystem.Net.sockets.socketIn_Clients.Values
  251. clientSocket.Send(value,SocketFlags.None)
  252. Next
  253. ReturnTrue
  254. CatchexAsException
  255. RaiseEventException(ex)
  256. ReturnFalse
  257. EndTry
  258. EndFunction
  259. #EndRegion
  260. '''<summary>
  261. '''得到客户端的Socket联接
  262. '''</summary>
  263. '''<paramname="IPAddress">客户端的IP</param>
  264. '''<paramname="Port">客户端的端口号</param>
  265. '''<returns>Socket联接</returns>
  266. '''<remarks></remarks>
  267. PrivateFunctionGetClientSocket(ByValIPAddressAsString,ByValPortAsString)AsSocket
  268. Try
  269. DimClientSocketAsSocket
  270. ClientSocket=_Clients.Item(IPAddress&"\&"&Port)
  271. ReturnClientSocket
  272. CatchexAsException
  273. RaiseEventException(ex)
  274. Returnnothing
  275. EndTry
  276. EndFunction
  277. #EndRegion
  278. PrivateClassClientCommunication
  279. PublicEventException(ByValexAsException)
  280. PrivateServerSocketAsNewSystem.Net.sockets.socket(AddressFamily.InterNetwork,ProtocolType.Tcp)
  281. PrivatemyClientSocketAsNewSystem.Net.sockets.socket(AddressFamily.InterNetwork,ProtocolType.Tcp)
  282. PrivatemyParentObjectAsTcpserver
  283. Privateoldbytes()AsByte
  284. Private_IPAddress,_PortAsString
  285. PrivateNclientInfoTAsIPEndPoint=nothing
  286. PrivateiLenAsInteger
  287. PrivateallDoneAsNewManualResetEvent(False)
  288. '''<summary>
  289. '''实例ClientCommunication类
  290. '''</summary>
  291. '''<paramname="ServerSocket"></param>
  292. '''<paramname="ClientSocket"></param>
  293. '''<paramname="ParentObject"></param>
  294. '''<remarks></remarks>
  295. PublicSubNew(ByValServerSocketAsSocket,ByValClientSocketAsSocket,ByValParentObjectAsTcpserver)
  296. Me.ServerSocket=ServerSocket
  297. myClientSocket=ClientSocket
  298. myParentObject=ParentObject
  299. NclientInfoT=CType(myClientSocket.RemoteEndPoint,IPEndPoint)
  300. _IPAddress=NclientInfoT.Address.ToString
  301. _Port=NclientInfoT.Port.ToString
  302. EndSub
  303. '''<summary>
  304. '''客户端通讯主线程
  305. '''</summary>
  306. '''<remarks></remarks>
  307. PublicSubserverThreadProc()
  308. Try
  309. DimsbAsNewSocketAndBuffer
  310. sb.socket=myClientSocket
  311. sb.socket.BeginReceive(sb.Buffer,sb.Buffer.Length,SocketFlags.None,AddressOfReceiveCallBack,sb)
  312. 'allDone.WaitOne()
  313. CatchexAsException
  314. RaiseEventException(ex)
  315. EndTry
  316. EndSub
  317. '''<summary>
  318. '''socket异步接收回调函数
  319. '''</summary>
  320. '''<paramname="ar"></param>
  321. '''<remarks></remarks>
  322. PrivateSubReceiveCallBack(ByValarAsIAsyncResult)
  323. DimsbAsSocketAndBuffer
  324. allDone.Set()
  325. sb=CType(ar.AsyncState,SocketAndBuffer)
  326. Try
  327. Ifsb.socket.ConnectedThen
  328. iLen=sb.socket.EndReceive(ar)
  329. IfiLen>0Then
  330. ReDimoldbytes(iLen-1)
  331. Array.copy(sb.Buffer,oldbytes,iLen)
  332. myParentObject.GetData_Byte(oldbytes,oldbytes.Length,_IPAddress,_Port)
  333. sb.socket.BeginReceive(sb.Buffer,sb)
  334. Else
  335. If(NotmyClientSocketIsnothing)Then
  336. IfmyClientSocket.ConnectedThen
  337. myClientSocket.Close()
  338. Else
  339. myClientSocket.Close()
  340. EndIf
  341. myClientSocket=nothing
  342. IfNotNclientInfoTIsnothingThen
  343. myParentObject._Clients.Remove(_IPAddress&"\&"&_Port)
  344. myParentObject.GetClientClose(_IPAddress,_Port)
  345. EndIf
  346. EndIf
  347. EndIf
  348. EndIf
  349. CatchexAsException
  350. If(NotmyClientSocketIsnothing)Then
  351. IfmyClientSocket.ConnectedThen
  352. myClientSocket.Close()
  353. Else
  354. myClientSocket.Close()
  355. EndIf
  356. myClientSocket=nothing
  357. IfNotNclientInfoTIsnothingThen
  358. myParentObject._Clients.Remove(_IPAddress&"\&"&_Port)
  359. myParentObject.GetClientClose(_IPAddress,_Port)
  360. EndIf
  361. EndIf
  362. RaiseEventException(ex)
  363. EndTry
  364. EndSub
  365. '''<summary>
  366. '''异步操作socket缓冲类
  367. '''</summary>
  368. '''<remarks></remarks>
  369. PrivateClassSocketAndBuffer
  370. PublicSocketAsSystem.Net.sockets.socket
  371. PublicBuffer(8192)AsByte
  372. EndClass
  373. EndClass
  374. EndClass


2)TCP 客户端连接通信类

  1. ImportsSystem.Net
  2. ImportsSystem.Net.sockets
  3. ImportsSystem.Threading
  4. PublicClassTCPClient
  5. #Region"私有成员"
  6. Private_LocationClientSocketAsSocket'本地侦听服务
  7. Private_LocalPortAsString'本地端口
  8. Private_LocalHostNameAsString
  9. Private_LocalIPAsString
  10. PrivateautoEventAsAutoResetEvent
  11. Private_RemoteHostNameAsString'遠程端計算機名
  12. Private_RemoteIPAsString'遠程端計算機IP
  13. Private_RemotePortAsString'遠程端計算機Port
  14. Private_RemoteIPOrHostNameAsString
  15. 'Private_MaxClientAsInteger'最大客户端连接数
  16. 'Private_ClientsAsNewSortedList'客户端队列
  17. 'Private_ListenThreadAsThread=nothing'侦听线程
  18. 'Private_ServerStartAsBoolean=False'服务器是否已经启动
  19. Private_RecvMaxAsInteger'接收缓冲区大小
  20. PrivateClientThreadAsThread
  21. 'PrivateClitenStreamAsNetworkStream
  22. PrivateIsstopAsBoolean=False
  23. #EndRegion
  24. #Region"事件"
  25. '''<summary>
  26. '''客户端联接事件
  27. '''</summary>
  28. '''<remarks></remarks>
  29. PublicEventClientConnected()
  30. '''<summary>
  31. '''客户端断开事件
  32. '''</summary>
  33. '''<remarks></remarks>
  34. PublicEventClientClosed()
  35. '''<summary>
  36. '''接收到客户端的数据
  37. '''</summary>
  38. '''<paramname="value">数据</param>
  39. '''<remarks></remarks>
  40. PublicEventDataArrived(ByValvalueAsByte(),ByValLenAsInteger)
  41. '''<summary>
  42. '''异常数据
  43. '''</summary>
  44. '''<paramname="ex"></param>
  45. '''<remarks></remarks>
  46. PublicEventException(ByValexAsException)
  47. #EndRegion
  48. #Region"属性"
  49. '''<summary>
  50. '''是否已經連接
  51. '''</summary>
  52. '''<value></value>
  53. '''<returns></returns>
  54. '''<remarks></remarks>
  55. PublicReadOnlyPropertyConnected()AsBoolean
  56. Get
  57. Return_LocationClientSocket.Connected
  58. EndGet
  59. EndProperty
  60. '''<summary>
  61. '''本地計算機名稱
  62. '''</summary>
  63. '''<value></value>
  64. '''<returns></returns>
  65. '''<remarks></remarks>
  66. PublicReadOnlyPropertyLocalHostName()AsString
  67. Get
  68. Return_LocalHostName
  69. EndGet
  70. EndProperty
  71. '''<summary>
  72. '''本地計算IP
  73. '''</summary>
  74. '''<value></value>
  75. '''<returns></returns>
  76. '''<remarks></remarks>
  77. PublicReadOnlyPropertyLocalIP()AsString
  78. Get
  79. Return_LocalIP
  80. EndGet
  81. EndProperty
  82. '''<summary>
  83. '''本地計算機端口
  84. '''</summary>
  85. '''<value></value>
  86. '''<returns></returns>
  87. '''<remarks></remarks>
  88. PublicReadOnlyPropertyLocalPort()AsString
  89. Get
  90. Return_LocalPort
  91. EndGet
  92. EndProperty
  93. '''<summary>
  94. '''遠程計算機IP
  95. '''</summary>
  96. '''<value></value>
  97. '''<returns></returns>
  98. '''<remarks></remarks>
  99. PublicReadOnlyPropertyRemoteIP()AsString
  100. Get
  101. Return_RemoteIP
  102. EndGet
  103. EndProperty
  104. '''<summary>
  105. '''遠程計算機端口
  106. '''</summary>
  107. '''<value></value>
  108. '''<returns></returns>
  109. '''<remarks></remarks>
  110. PublicReadOnlyPropertyRemotePort()AsString
  111. Get
  112. Return_RemotePort
  113. EndGet
  114. EndProperty
  115. '''<summary>
  116. '''遠程計算機名稱
  117. '''</summary>
  118. '''<value></value>
  119. '''<returns></returns>
  120. '''<remarks></remarks>
  121. PublicReadOnlyPropertyRemoteHostName()AsString
  122. Get
  123. Return_RemoteHostName
  124. EndGet
  125. EndProperty
  126. #EndRegion
  127. #Region"方法"
  128. '''<summary>
  129. '''实例 Tcpserver
  130. '''</summary>
  131. '''<paramname="RemoteIPOrHostName">需要連接服務的IP地址或計算機名稱</param>
  132. '''<paramname="Port">侦听客户端联接的端口号</param>
  133. '''<paramname="RecvMax">接收缓冲区大小</param>
  134. '''<paramname="RecvSleep">接收线程睡眠时间</param>
  135. '''<remarks></remarks>
  136. SubNew(ByValRemoteIPOrHostNameAsString,ByValPortAsString,ByValRecvSleepAsInteger)
  137. Try
  138. _LocalHostName=Dns.GetHostName()
  139. '_RemoteIP=Dns.GetHostAddresses(RemoteIPOrHostName)(0).ToString
  140. _RemotePort=Port
  141. _RecvMax=RecvMax
  142. _RemoteIPOrHostName=RemoteIPOrHostName
  143. _RemotePort=Port
  144. 'DimstrServerHostAsNewIPEndPoint(IPAddress.Any,Int32.Parse(_ListenPort))
  145. '建立TCP侦听
  146. _LocationClientSocket=NewSocket(AddressFamily.InterNetwork,ProtocolType.Tcp)
  147. autoEvent=NewAutoResetEvent(False)
  148. DimcThreadAsNewThread(NewThreadStart(AddressOfConnectHost))
  149. cThread.Start()
  150. autoEvent.WaitOne(1000,False)
  151. cThread.Abort()
  152. CatchexAsException
  153. RaiseEventException(ex)
  154. EndTry
  155. EndSub
  156. PublicSubConnectHost()
  157. Try
  158. DimremoteIPAsNet.IPAddress=nothing
  159. IfIPAddress.TryParse(_RemoteIPOrHostName,remoteIP)Then
  160. '_LocationClientSocket.BeginConnect()
  161. _LocationClientSocket.Connect(remoteIP,_RemotePort)
  162. '_RemoteIP=RemoteHostName
  163. Else
  164. _LocationClientSocket.Connect(_RemoteIPOrHostName,_RemotePort)
  165. _RemoteHostName=RemoteHostName
  166. EndIf
  167. If_LocationClientSocket.ConnectedThen
  168. _LocationClientSocket.SendBufferSize=_RecvMax
  169. _LocationClientSocket.ReceiveBufferSize=_RecvMax
  170. DimclientInfoTAsIPEndPoint
  171. clientInfoT=CType(_LocationClientSocket.RemoteEndPoint,IPEndPoint)
  172. _RemoteIP=clientInfoT.Address.ToString
  173. 'DimremoteHostAsNet.IPHostEntry
  174. _RemoteHostName=Dns.GetHostEntry(_RemoteIP).HostName
  175. clientInfoT=CType(_LocationClientSocket.LocalEndPoint,IPEndPoint)
  176. _LocalIP=clientInfoT.Address.ToString
  177. _LocalPort=clientInfoT.Port.ToString
  178. Isstop=False
  179. RaiseEventClientConnected()
  180. ClientThread=NewThread(NewThreadStart(AddressOfClientListen))
  181. ClientThread.Start()
  182. autoEvent.Set()
  183. EndIf
  184. CatchexAsException
  185. EndTry
  186. EndSub
  187. '''<summary>
  188. '''關閉客戶端連接
  189. '''</summary>
  190. '''<remarks></remarks>
  191. PublicSubClose()
  192. Try
  193. If_LocationClientSocketIsnothingThenExitSub
  194. Isstop=True
  195. IfNotClientThreadIsnothingThen
  196. Thread.Sleep(5)
  197. ClientThread.Abort()
  198. EndIf
  199. _LocationClientSocket.Close()
  200. _LocationClientSocket=nothing
  201. ClientThread=nothing
  202. RaiseEventClientClosed()
  203. CatchexAsException
  204. RaiseEventException(ex)
  205. EndTry
  206. EndSub
  207. '''<summary>
  208. '''实例 Tcpserver
  209. '''</summary>
  210. '''<paramname="value">發送的資料,二進制數組</param>
  211. '''<remarks></remarks>
  212. PublicFunctionSendData(ByValvalueAsByte())AsBoolean
  213. Try
  214. _LocationClientSocket.Send(value)
  215. CatchexAsException
  216. RaiseEventException(ex)
  217. EndTry
  218. EndFunction
  219. PrivateSubClientListen()
  220. DimtmpByt(8192)AsByte
  221. DimrecData()AsByte
  222. DimRAsInteger
  223. WhileNotIsstop
  224. Try
  225. If_LocationClientSocket.Poll(50,SelectMode.SelectWrite)Then
  226. R=_LocationClientSocket.Receive(tmpByt)
  227. IfR>0Then
  228. ReDimrecData(R-1)
  229. Array.copy(tmpByt,recData,R)
  230. RaiseEventDataArrived(recData,recData.Length)
  231. Else
  232. If(Not_LocationClientSocketIsnothing)Then
  233. _LocationClientSocket.Close()
  234. _LocationClientSocket=nothing
  235. Isstop=True
  236. RaiseEventClientClosed()
  237. EndIf
  238. EndIf
  239. EndIf
  240. CatchsexAsSocketException
  241. Ifsex.ErrorCode=10054Then
  242. If(Not_LocationClientSocketIsnothing)Then
  243. _LocationClientSocket.Close()
  244. _LocationClientSocket=nothing
  245. Isstop=True
  246. RaiseEventClientClosed()
  247. EndIf
  248. Else
  249. RaiseEventException(sex)
  250. EndIf
  251. CatchexAsException
  252. RaiseEventException(ex)
  253. EndTry
  254. EndWhile
  255. EndSub
  256. #EndRegion
  257. EndClass

窗体调用TCP 类如下

服务器端:

  1. PublicWithEventsMyClientAsTcpserver

客户机端:
  1. PublicWithEventsMyClientAsTCPClient

原文地址:https://www.jb51.cc/vb/258325.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐