Click here to Skip to main content
13,404,408 members (41,212 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 29 Jun 2003

A word on asynchronous sockets

, 29 Jun 2003
Rate this:
Please Sign up or sign in to vote.
A discussion on asynchronous sockets


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; /*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.


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


About the Author

khaled chahabeddin
Web Developer
Saudi Arabia Saudi Arabia
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSending DataTable from server to client Pin
saeedazam7869-Jan-13 4:53
membersaeedazam7869-Jan-13 4:53 
General[Message Deleted] Pin
it.ragester2-Apr-09 22:53
memberit.ragester2-Apr-09 22:53 
GeneralSendTo & Listen Pin
CentrumZ19-Jul-05 23:50
memberCentrumZ19-Jul-05 23:50 
QuestionGot MSDN? Pin
Anonymous9-Dec-04 4:58
sussAnonymous9-Dec-04 4:58 
GeneralWrong category Pin
Nemanja Trifunovic30-Jun-03 7:35
memberNemanja Trifunovic30-Jun-03 7: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1802120.2 | Last Updated 30 Jun 2003
Article Copyright 2003 by khaled chahabeddin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid