
Originally Posted by
towmtdj
Hi everybody
Hi.

Originally Posted by
towmtdj
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;
//...
}