Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#
Article

A word on asynchronous sockets

Rate me:
Please Sign up or sign in to vote.
1.78/5 (19 votes)
29 Jun 20032 min read 72.9K   28   5
A discussion on asynchronous sockets

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 :-

C#
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 :-

C#
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

C#
private static void MethodName (IAsyncResult result) 
{

  Socket server= (Socket) result.AsyncState; /*down cast*/ 
  Socket client = server.EndAccept (result); /*call EndAccept*/

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSending DataTable from server to client Pin
saeedazam7869-Jan-13 3:53
saeedazam7869-Jan-13 3:53 
General[Message Deleted] Pin
it.ragester2-Apr-09 21:53
it.ragester2-Apr-09 21:53 
GeneralSendTo & Listen Pin
CentrumZ19-Jul-05 22:50
CentrumZ19-Jul-05 22:50 
QuestionGot MSDN? Pin
Anonymous9-Dec-04 3:58
Anonymous9-Dec-04 3:58 
GeneralWrong category Pin
Nemanja Trifunovic30-Jun-03 6:35
Nemanja Trifunovic30-Jun-03 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.