以下是我在客户端的套接字编程中使用的回调方法,用于从服务器异步接收数据:-
Public Sub Connect()
m_clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
m_clientSocket.Connect(Ip, PortNo)
If (m_clientSocket.Connected) Then
m_clientSocket.BeginReceive(dataBuffer, 0,dataBuffer.Length SocketFlags.None, New AsyncCallback(AddressOf OnDataReceived), m_clientSocket) //here I connect the callback OnDataRecieved
End If
End Sub
Public Sub OnDataRecieved(ByVal async As IAsyncResult)
Dim Recieved_Size As Integer = m_clientSocket.EndReceive(async)
' rest of the code ...
End Sub
我的疑问是,当在“OnDataReceived 方法”“Received_Size”= 0 内部时,我是否应该断定套接字已断开连接?如果不是,那么在什么情况下 Received_Size 应该 = 0 ?
为什么我要问的是,每当连接的服务器断开连接时,就会调用“OnDataReceived”方法,结果“Received_Size”为= 0。但只有在有一些数据要为套接字读取时才应该调用它,而不是在连接时调用/disconnect 发生了,对吧?
我非常感谢任何能澄清我的疑问的人。