tags:

views:

472

answers:

3

Hey, I'm having nightmares with C# asynchronous socket programming. Can you point me to a guide or tutorial or a book which explains in depth asynchronous programming. If possibly contains how to use the state parameter in Begin Send/Receive functions to handle multiple clients.

+5  A: 

You can read

Asynchronous Socket Programming in C# Part 1

and

Asynchronous Socket Programming in C#: Part II

These are results of a quick Google search.

rahul
yes, I have found all those links, but I need to learn more than that
hab
Can you be more specific? I think the example given above demonstrates how you can write a client/server using async pattern, and also with a state parameter.
feroze
A: 

check this

ArsenMkrt
+2  A: 

Server can be organized by such scenario: separeted thread listening port for client connections.

 
 /// Async server
 public class AsyncServer
 {
     /// Server socket
     private Socket _serverSocket;

     /// Element for sync wait 
     private static ManualResetEvent _connectionMutex =
              new ManualResetEvent(false);

     /// Client handler
     private ClientManager _clientManager;

     public AsyncServer(string ipAddrees, int port)
     {
         try
         {             
             this._serverSocket = new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream, ProtocolType.Tcp);

             this._serverSocket.Bind(
               new IPEndPoint(IPAddress.Parse(ipAddrees), port));

         }
         catch (Exception ex)
         {
             throw new Exception("Server Init Error.", ex);
         }
     }

     private BackgroundWorker _listenThread = new BackgroundWorker();

     public void Start()
     {
         this._clientManager = new ClientManager(this._clientConnections);

         this._listenThread.WorkerReportsProgress = true;
         this._listenThread.WorkerSupportsCancellation = true;
         this._listenThread.DoWork +=
              new DoWorkEventHandler(ListenThread_DoWork);

         this._listenThread.RunWorkerAsync(this._serverSocket);
     }

     /// Thread for listening port
     private void ListenThread_DoWork(object sender, DoWorkEventArgs e)
     {
         Socket serverSocket = (Socket)e.Argument;

         serverSocket.Listen(100);

         while (true)
         {
             // reset mutex
             _connectionMutex.Reset();

             serverSocket.BeginAccept(
             new AsyncCallback(this.AcceptCallback), this._serverSocket);

             // waiting for the next connection
             _connectionMutex.WaitOne();
         }
     }

     /// List of client connections
     private List _clientConnections = new List();  

     public int ConnectionsCount
     {
         get { return this._clientConnections.Count; }
     }

     /// Callback method for handling connections
     private void AcceptCallback(IAsyncResult asyncResult)
     {
         _connectionMutex.Set();

         Socket serverSocket = (Socket)asyncResult.AsyncState;
         Socket clientSocket = (Socket)serverSocket.EndAccept(asyncResult);
         this._clientConnections.Add(clientSocket);

         this._clientManager.HandleClient(clientSocket);
     }

 }
 

Method AcceptCallback(IAsyncResult asyncResult) handle all new client connections and transfer to ClientManager, which create for each client separate thread.

 

   public class ClientManager
   {       

       private List _clientProcessors = new List();


       private List _connections;       

       public ClientManager(List connections)
       {
           this._connections = connections;
       }

       /// Handling of client connection      
       public void HandleClient(Socket clientSocket)
       {
           BackgroundWorker clientProcessor = new BackgroundWorker();
           clientProcessor.DoWork += new DoWorkEventHandler(ClientProcessing);

           this._clientProcessors.Add(clientProcessor);

           List args = new List();
           // 
           // args.Add(...);           

           clientProcessor.RunWorkerAsync(args);
       }      

       private void ClientProcessing(object sender, DoWorkEventArgs e)
       {
           // reading args
           List args = (List)e.Argument;          

           ProtocolSerializer serializer = new ProtocolSerializer();

           try
           {
               while (socket.Connected)
               {               
                   // ...

               }
           }
           catch (SocketException)
           {
              // ...
           }
           catch (Exception)
           {
              // ...
           }
       }
   }
merin