CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Problems with BeginReceive Callback

    Quote Originally Posted by towmtdj View Post
    Hi everybody
    Hi.
    Quote Originally Posted by towmtdj View Post
    What am I doing wrong ?
    Well, it would help if you posted some code - in particular, the call to BeginReceive(), and the corresponding callback method.
    Are you sure you've passed the right object and that you're checking in the right callback, or that there aren't any other methods invoking the same callback, possibly sending a Socket instead, and you don't expect it?

    Take a look at the MSDN example at this page.
    Here's the condensed version:
    Code:
    // some class that holds some user data, like the socket, or the buffer, and whatever
    public class StateObject
    {
         // ...
    }
    Code:
    // A listen callback, invoked by BeginAccept(), which passed a socket as user state object
    public static void Listen_Callback(IAsyncResult ar)
    {
         //...
         Socket s = (Socket) ar.AsyncState;        // retrieve the socket (normally, you know what type to cast to)
         Socket s2 = s.EndAccept(ar);                  // end operation, get the new socket
         StateObject state = new StateObject();    // create and init the StateObject instance
         state.workSocket = s2;
         s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0,
    	                       new AsyncCallback(Read_Callback), state);	// and pass it instead
    }
    Code:
    // the read callback retrieves the StateObject instance
    public static void Read_Callback(IAsyncResult ar)
    {
    	StateObject state = (StateObject) ar.AsyncState;
    	
    	//...
    }
    Last edited by TheGreatCthulhu; August 1st, 2012 at 07:36 AM.

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

Featured