|
-
Mar 4th, 2008, 02:40 PM
#1
Thread Starter
Lively Member
Need Help with Socket.BeginReceive routine
I've been reading the documentation from Microsoft regarding the Asynchronous Socket.Beginreceive method......
According to Microsoft... the following code works....
Code:
Private Shared Sub Receive(ByVal client As Socket)
' Create the state object.
Dim state As New StateObject
state.workSocket = client
' Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
End Sub 'Receive
Private Shared Sub ReceiveCallback(ByVal ar As IAsyncResult)
' Retrieve the state object and the client socket
' from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
' Read data from the remote device.
Dim bytesRead As Integer = client.EndReceive(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
' Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Else
' All the data has arrived; put it in response.
If state.sb.Length > 1 Then
response = state.sb.ToString()
End If
' Signal that all bytes have been received.
receiveDone.Set()
End If
End Sub 'ReceiveCallback
It's supposed to be re-entrant depending on the bytes received.
BUT, I'm using the same code with little modifications....and the ReceiveDone.Set NEVER gets called. So my program is blocking while waiting for the Set signal.
I've attached my code below..
Code:
Private Sub Receive(ByVal client As Socket)
' Create the state object.
Dim state As New StateObject
state.workSocket = client
' Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
End Sub 'Receive
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
' Retrieve the state object and the client socket
' from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
Dim lstrTemp As String
' Read data from the remote device.
Dim bytesRead As Integer = client.EndReceive(ar)
If bytesRead > 0 Then
Try
' There might be more data, so store the data received so far.
lstrTemp = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
state.sb.Append(lstrTemp)
' Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Catch
lstrTemp = String.Empty
End Try
Else
If state.sb.Length > 1 Then
Host_Response(state.sb.ToString)
End If
ReceiveDone.Set()
End If
What am I doing wrong???
-
Mar 4th, 2008, 02:58 PM
#2
Re: Need Help with Socket.BeginReceive routine
You say that your modified version of the code doesnt work, but have you verified that the original code works?
Why are you wrapping this part of the code in a try-catch block?
vb Code:
Try
' There might be more data, so store the data received so far.
lstrTemp = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
state.sb.Append(lstrTemp)
' Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Catch
lstrTemp = String.Empty
End Try
-
Mar 4th, 2008, 03:02 PM
#3
Thread Starter
Lively Member
Re: Need Help with Socket.BeginReceive routine
 Originally Posted by Atheist
You say that your modified version of the code doesnt work, but have you verified that the original code works?
Why are you wrapping this part of the code in a try-catch block?
vb Code:
Try
' There might be more data, so store the data received so far.
lstrTemp = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
state.sb.Append(lstrTemp)
' Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Catch
lstrTemp = String.Empty
End Try
Hey Atheist,
Well to tell you the truth, I haven't verified that their code works, but when I do a google on the method, all the example code from others looks similar.
I've wrapped the code in a try...catch...but no exceptions are ever thrown.
Totally lost here.
-
Mar 4th, 2008, 03:06 PM
#4
Re: Need Help with Socket.BeginReceive routine
Remove the try-catch block. Now step through the code and see what happens.
-
Mar 4th, 2008, 03:12 PM
#5
Thread Starter
Lively Member
Re: Need Help with Socket.BeginReceive routine
 Originally Posted by Atheist
Remove the try-catch block. Now step through the code and see what happens.
Ok, removed the try...catch block. No Joy. When I step thru the code,
I get data returned, so bytesRead is great than 0 .... I then get the response, and call Beginreceive again....but what happens is that the code just drops thru to the end of the function. It doesn't re-enter and try to do the Dim bytesRead As Integer = client.EndReceive(ar) statement. So, the ReceiveDone.set statement is never performed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|