
08-03-2012, 06:44 AM
|
|
Newcomer
|
|
Join Date: Jun 2011
Posts: 7
|
|
Regarding Callback method used for BeginReceive method
|
Hi everyone,
Following is the callback method that I use in my socket programming at client side for async receiving of data from server:-
Code:
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
What my doubt is that when inside "OnDataRecieved method" "Recieved_Size" = 0, should I conclude that the socket has got disconnected? If not, then under what cases should Recieved_Size be = 0 ?
Why I am asking is that whenever the connected server gets disconnected then "OnDataRecieved" method gets called and the resulting "Recieved_Size" is = 0. But it should be called only when there is some data to be read for the socket, not when connect/disconnect happens, right?
I would really appreciate anyone who would clarify my doubt. Thanks in advance.
|
|