I have been given a code that also involves an asynchronous socketing which I am begginer at. I do not know why the object state as a second parameter of BeginAccept socket is null and to be honest, I did not get the use of this parameter from MSDN. And also, why the BeginAccept is being called again from the callback method?
public void Start()
{
this.mTcpListener.Start();
this.mTcpListener.BeginAcceptSocket(this.AcceptClient, null);
}
protected void AcceptClient(IAsyncResult ar)
{
if (this.mTcpListener != null)
{
System.Net.Sockets.Socket s = this.mTcpListener.EndAcceptSocket(ar);
Client c = new Client(this, s, this.GetFreePlayerID());
..some code for adding the client instance to collection....
this.mTcpListener.BeginAcceptSocket(this.AcceptClient, null);
}
}
Following on from fsimonazzi, I assume that this code is part of a server which is meant to hold many incoming connections.
BeginAccept is used to start the accept process on a socket.
In AcceptClient it then finishes the AcceptConnection async process with EndAcceptSocket.
At this point the program is no longer listening for new connections/sockets. In order to do that, it again starts an async BeginAcceptSocket process.