What is the best way to accept new sockets in async way.
First way:
while (!abort && listener.Server.IsBound)
{
acceptedSocketEvent.Reset();
listener.BeginAcceptSocket(AcceptConnection, null);
bool signaled = false;
do
{
signaled = acceptedSocketEvent.WaitOne(1000, false);
} while (!signaled && !abort && listener.Server.IsBound);
}
where AcceptConnection should be:
private void AcceptConnection(IAsyncResult ar)
{
// Signal the main thread to continue.
acceptedSocketEvent.Set();
Socket socket = listener.EndAcceptSocket(ar);
// continue to receive data and so on...
....
}
or Second way:
listener.BeginAcceptSocket(AcceptConnection, null);
while (!abort && listener.Server.IsBound)
{
Thread.Sleep(500);
}
and AcceptConnection will be:
private void AcceptConnection(IAsyncResult ar)
{
Socket socket = listener.EndAcceptSocket(ar);
// begin accepting next socket
listener.BeginAcceptSocket(AcceptConnection, null);
// continue to receive data and so on...
....
}
What is your prefered way and why?