Which socket to use?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is the code example on the MSDN page for the Socket.BeginReceive function:

allDone.Set();
Socket s = (Socket) ar.AsyncState;
Socket s2 = s.EndAccept(ar);
StateObject so2 = new StateObject();
so2.workSocket = s2;
s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0,
new AsyncCallback(Async_Send_Receive.Read_Callback),
so2);

I have a few questions about this code example:
1) I have seen allDone.Set() on quite a few pages on MSDN examples - what
object is it and what is its purpose?
2) Which of the sockets returned by ar.AsyncState or s.EndAccept() should I
use? What is the difference between them if any?
 
Joachim said:
This is the code example on the MSDN page for the Socket.BeginReceive
function:

allDone.Set();
Socket s = (Socket) ar.AsyncState;
Socket s2 = s.EndAccept(ar);
StateObject so2 = new StateObject();
so2.workSocket = s2;
s2.BeginReceive(so2.buffer, 0, StateObject.BUFFER_SIZE,0,
new AsyncCallback(Async_Send_Receive.Read_Callback),
so2);

I have a few questions about this code example:
1) I have seen allDone.Set() on quite a few pages on MSDN examples - what
object is it and what is its purpose?

It's a ManualResetEvent object I guess. Take a look at the BeginAccept MSDN
page. They use it to prevent multiple calls to BeginAccept at the same
time.
2) Which of the sockets returned by ar.AsyncState or s.EndAccept() should
I use? What is the difference between them if any?

Your example code above should be placed into the BeginAccept callback
function. Then "s" is the listening socket, and "s2" is the socket of a new
incoming connection. So you should read/write to s2.

They should have chosen better example code for these functions imo.

hth,
Max
 
Back
Top