Click here to Skip to main content
Click here to Skip to main content

A word on asynchronous sockets

, 29 Jun 2003
Rate this:
Please Sign up or sign in to vote.
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 :-

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.

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

Share

About the Author

khaled chahabeddin
Web Developer
Saudi Arabia Saudi Arabia
No Biography provided

Comments and Discussions

 
QuestionSending DataTable from server to client Pinmembersaeedazam7869-Jan-13 3:53 
General[Message Deleted] Pinmemberit.ragester2-Apr-09 21:53 
GeneralSendTo & Listen PinmemberCentrumZ19-Jul-05 22:50 
QuestionGot MSDN? PinsussAnonymous9-Dec-04 3:58 
GeneralWrong category PinmemberNemanja Trifunovic30-Jun-03 6:35 

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

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

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