Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Flow control using Asynch Sockets

Flow control using Asynch Sockets

Scheduled Pinned Locked Moved C#
helpperformancesysadminquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TimSWatson
    wrote on last edited by
    #1

    Hey Im having a problem using the sockets asynchronous BeginSend method. My server application is sending a large amount of data in small chunks and therefore I am calling BeginSend many times in a short period of time. I believe that the recieving application cant handle the speed and therefore the sender is queuing the calls until they can be sent. This means that memory is constantly increasing on the sender application. I think that I need to implement some sort of flow control to stop calling BeginSend when there are to many calls pending. Can anyone help me out or point me to some literature on this issue? Thanks

    J 1 Reply Last reply
    0
    • T TimSWatson

      Hey Im having a problem using the sockets asynchronous BeginSend method. My server application is sending a large amount of data in small chunks and therefore I am calling BeginSend many times in a short period of time. I believe that the recieving application cant handle the speed and therefore the sender is queuing the calls until they can be sent. This means that memory is constantly increasing on the sender application. I think that I need to implement some sort of flow control to stop calling BeginSend when there are to many calls pending. Can anyone help me out or point me to some literature on this issue? Thanks

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      This is the pattern I'm familiar with (in pseudocode):

      Socket socket;
      Queue<Data> pendingData;
      bool sending;

      public void SendAsync (Data data)
      {
      if (sending)
      {
      pendingData.Add(data);
      }
      else
      {
      sending = true;
      socket.BeginSend(data, new Callback(SendCallback));
      }
      }

      private void SendCallback ()
      {
      socket.EndSend();

      if (pendingData.Count > 0)
      {		
          socket.BeginSend(pendingData.DequeueReasonableAmountOfData(), new Callback(SendCallback));
      }
      else
      {
          sending = false;
      }
      

      }

      This maintains only one outstanding asynchronous send operation at a time and the amount of data queued to be sent can easily be handled by user code. This is useful in case the queued data needs to be resent if the client disconnects/reconnects, or if you want to put a limit on how much data to queue to send or perhaps prioritize the queued data in some way. Of course another important part would be to make sure the receiver is receiving in such a way that parsing the data isn't blocking the socket from immediately receiving the next packet being sent.

      :badger:

      T 1 Reply Last reply
      0
      • J Jimmanuel

        This is the pattern I'm familiar with (in pseudocode):

        Socket socket;
        Queue<Data> pendingData;
        bool sending;

        public void SendAsync (Data data)
        {
        if (sending)
        {
        pendingData.Add(data);
        }
        else
        {
        sending = true;
        socket.BeginSend(data, new Callback(SendCallback));
        }
        }

        private void SendCallback ()
        {
        socket.EndSend();

        if (pendingData.Count > 0)
        {		
            socket.BeginSend(pendingData.DequeueReasonableAmountOfData(), new Callback(SendCallback));
        }
        else
        {
            sending = false;
        }
        

        }

        This maintains only one outstanding asynchronous send operation at a time and the amount of data queued to be sent can easily be handled by user code. This is useful in case the queued data needs to be resent if the client disconnects/reconnects, or if you want to put a limit on how much data to queue to send or perhaps prioritize the queued data in some way. Of course another important part would be to make sure the receiver is receiving in such a way that parsing the data isn't blocking the socket from immediately receiving the next packet being sent.

        :badger:

        T Offline
        T Offline
        TimSWatson
        wrote on last edited by
        #3

        Thanks this is exactly what I needed.

        J 1 Reply Last reply
        0
        • T TimSWatson

          Thanks this is exactly what I needed.

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          You're welcome :)

          :badger:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups