views:

28

answers:

1

I have a receive callback from an async client method that is supposed to call socket.BeginReceive internally to check if data is done being sent. The code is as follows:

    private void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;
            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0)
            {
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
                    new AsyncCallback(ReceiveCallback), state);
            }
            else
            {
                if (state.sb.Length > 1)
                {
                    response = state.sb.ToString();
                }
                File.AppendAllText(DataDisplay.Properties.Settings.Default.rawDataLog, response + "\r\n");
                receiveDone.Set();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

The problem with this is that when the code reaches the client.BeginReceive call, nothing happens. I know that sounds vague, so let me try to explain. I have set breakpoints throughout the code to see where it could possibly be going, and I can't figure it out. I have tried the else case, the beginning of the ReceiveCallback, where the receivedone.Set() returns to in the main code, the catch statement, etc. to no avail.

The part that is most confusing is that when I run it with a test server I wrote, it works as I would expect. This code is essentially from the MSDN example on async sockets, in case it looks familiar. Only when I try it with the third party device, I get stymied.

Any ideas what could be going on here?

+1  A: 

Is it possible that while you are debugging, nothing is actually being received so its essentially permanently waiting on the receive thread? Note that BeginReceive will use a ThreadPool thread, and will callback to the method you post. If its not calling back, then my first impression is that its not received anything.

Adam
When I probe the StateObject, it has received a string of data and has stored it, and the code does get to the point where it attempts to call client.BeginReceive again. Shouldn't at least re-enter the ReceiveCallback, even if there is nothing else left, or am I confused?
CaffeineZombie
If there isn't a timeout specified, it will wait - not sure about the underlying implementation.
Adam
I'd advise reviewing the documentation about how to use the method:http://msdn.microsoft.com/en-us/library/dxkwh6zw.aspx
Adam
The callback in this instance is only used when the method is ready to finish - this will usually be the cause in all async patterns, as the purpose of the callback is to stop you needing to wait on the finish or poll - you can be notified. In your example, its not finishing because its waiting for a buffers read (or the read to complete, not sure how its implemented).
Adam
Ah, I see what you mean. I think this puts me on track. This all gets quite confusing sometimes. Thank you very much for your help.
CaffeineZombie