title: Periodically send data from server to clients
body: <p>I have created an asynchronous server that can read in messages from any number of clients.  My server code is closely related to this example: <a href="http://msdn.microsoft.com/en-us/library/fx6588te.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/fx6588te.aspx</a></p>
  
  <p>After a client connects I would like to periodically send out data to that client, maybe 2 or 3 times per second.  I can't think of a good way to achieve this with an asynchronous send().  I'm assuming that some sort of timing mechanism would need to be added to my <code>AcceptCallback()</code> method because that is where the connection to the client happens.</p>
  
  <p>In the previous version of my server I used blocking sockets and just looped my <code>send()</code> method in an infinite <code>while()</code> loop and called <code>sleep()</code> to get the timing down.</p>
  
  <p>Here's what I'm thinking:</p>
  
  <pre><code>public void AcceptCallback(IAsyncResult ar)
      {
          allDone.Set(); // Signal the main thread to continue.
  
          // Get the socket that handles the client request.
          Socket listener = (Socket)ar.AsyncState;
          Socket handler = listener.EndAccept(ar);
          IPEndPoint remotePoint = (IPEndPoint)handler.RemoteEndPoint;
          IPAddress remoteAddress = remotePoint.Address;
          Console.WriteLine("Connected to {0}!", remoteAddress.ToString());
  
          // Create the state object.
          StateObject state = new StateObject();
          state.workSocket = handler;
  
          //Start periodically sending here??
  
          handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
              new AsyncCallback(ReadCallback), state);
      }
  </code></pre>