Introduction
Blocking is unacceptable when it comes to windows application. One way of dealing with that is through asynchronous methods provided by the Sockets namespace.
BeginAccept (...)
to accept an incoming connection
BeginConnect (...)
to connect to a remote host
BeginReceive (...)
to receive data from a socket
BeginReceiveFrom (...)
to receive data from a host (UDP)
BeginSend (...)
to send data to a socket
BeginSendTo (...)
to send data to a host (UDP)
How do they differ from the non asynchronous methods?
Generally speaking, we pass the same arguments of the regular method [non asynchronous] and we include some extra parameters such as a delegate, an object, and some other parameter depending on the method we’re using. Unlike the regular methods asynchronous methods don’t block at the BeginXXX
method, they do at the EndXXX
method. It’s like moving the blocking out to the method we pass to the delegate mentioned above.
Further explanation
As mentioned before, we pass the normal parameters we usually pass to the beginXXX method only now we add an AsyncCallback delegate and we pass our method to it. We also add an object; actually this object is mainly used to pass anything that might be useful to the method passed to the delegate. If you don’t like using this parameter just pass null and use a global static field instead, but its better not to do that.
BeginAccept and EndAccept
In general the BeginAccept
and the EndAccept
method looks like :-
IAsyncResult BeginAccept (AsyncCallback callBack, Object state)
Socket EndAccept (IAsyncResult result)
For the server side the code would look something like :-
Socket server = new Socket (AddressFamily.InterNetwok,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint (IPAddress.Any, 9050);
server.Bind (localEP);
server.Listen (10);
server.BeginAccept (new AsyncCallback (MethodName), server);
Of course our method must be of the form :-
Private static void MethodName (IAsyncResult result) {}
in order to match the delegate. The result returned by the BeginAccept
is passed to our method as an object of a class implementing the IAsyncResult
namely “result”.
How to use it
private static void MethodName (IAsyncResult result)
{
Socket server= (Socket) result.AsyncState;
Socket client = server.EndAccept (result);
Note: The same idea applies for rest of the asynchronous methods. What varies is the signature. As you can see, we put the EndAccept
in our method. The call of EndAccept
here will block, waiting for a connection. We pass an IAsyncResult
result, which is the same thing we got back from calling BeginAccept
, to the EndAccept
. The EndAccept
will return an auxiliary socket which we then use in the communication.
Finally
Using asynchronous methods will allow both the server and the client to send and receive asynchronously, Giving a more natural way of communication. Also, note that using asynchronous methods is kind of a substitution for multiple threading, meaning that we could still get that same result using multiple threads. Asynchronous methods relive you from dealing with threads.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.