why does this codesnippet block until the acceptcallback
has completed. According to the microsoft documentation
the beginaccept callback method executes in a separate
thread, and is called immediatly after BeginAccept
returns. I am trying to authenticate the client in this
callback. What am i doing wrong ?
sing System;
using System.Net;
using System.Text;
using System.Net.Sockets;
namespace SocketProblem
{
public class MainX
{
private static void StartListening()
{
IPEndPoint localEndPoint = new
IPEndPoint(IPAddress.Loopback, 5555);
Socket listener = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,
ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(1000);
listener.BeginAccept(new
AsyncCallback(AcceptCallback),listener);
}
// blocks until this method completes!
private static void AcceptCallback
(IAsyncResult ar){
Socket listener = (Socket)
ar.AsyncState;
Socket handler = listener.EndAccept
(ar);
listener.BeginAccept(new
AsyncCallback(AcceptCallback),listener);
handler.Send(Encoding.UTF8.GetBytes
("Welcome\r\n"));
//etc etc
byte[] buffer = new byte[512];
handler.Receive(buffer);
//etc etc
}
[STAThread]
static void Main(string[] args)
{
StartListening();
Console.WriteLine("Press <enter>
to stop");
Console.ReadLine();
}
}
}