Results 1 to 5 of 5

Thread: Need Help with Socket.BeginReceive routine

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    69

    Angry 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???

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Try
    2.                     ' There might be more data, so store the data received so far.
    3.                     lstrTemp = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
    4.                     state.sb.Append(lstrTemp)
    5.  
    6.                     ' Get the rest of the data.
    7.                     client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
    8.  
    9.                 Catch
    10.                     lstrTemp = String.Empty
    11.                 End Try
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    69

    Re: Need Help with Socket.BeginReceive routine

    Quote 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:
    1. Try
    2.                     ' There might be more data, so store the data received so far.
    3.                     lstrTemp = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
    4.                     state.sb.Append(lstrTemp)
    5.  
    6.                     ' Get the rest of the data.
    7.                     client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
    8.  
    9.                 Catch
    10.                     lstrTemp = String.Empty
    11.                 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.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Need Help with Socket.BeginReceive routine

    Remove the try-catch block. Now step through the code and see what happens.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    69

    Re: Need Help with Socket.BeginReceive routine

    Quote 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
  •  



Click Here to Expand Forum to Full Width