分享
 
 
 

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

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

这个类还没有完全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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有