title: How to reduce cpu usage during data transfer on TCP ports realtime
body: <p>I have a socket program which acts like both client and server.</p>
  
  <p>It initiates connection on an input port and reads data from it. On a real time scenario it reads data on input port and sends the data (record by record ) on to the output port.</p>
  
  <p>The problem here is that while sending data to the output port CPU usage increases to 50% while is not permissible.</p>
  
  <pre><code>while(1)
  {    
          if(IsInputDataAvail==1)//check if data is available on input port
          { 
             //condition to avoid duplications while sending
          if( LastRecordSent &lt; LastRecordRecvd )   
          {     
             record_time temprt;
             list&lt;record_time&gt; BufferList;
             list&lt;record_time&gt;::iterator j;
             list&lt;record_time&gt;::iterator i;
  
             // Storing into a temp list
              for(i=L.begin(); i != L.end(); ++i)
               {
                 if((i-&gt;recordId &gt; LastRecordSent) &amp;&amp; (i-&gt;recordId &lt;= LastRecordRecvd))
                 {     
                  temprt.listrec = i-&gt;listrec;
                  temprt.recordId = i-&gt;recordId;
                  temprt.timestamp = i-&gt;timestamp;
                  BufferList.push_back(temprt);
                 }
               }
  
                  //Sending to output port
               for(j=BufferList.begin(); j != BufferList.end(); ++j)
               {
                 LastRecordSent = j-&gt;recordId;
  
                std::string newlistrecord = j-&gt;listrec;
                newlistrecord.append("\n");
                char* newrecord= new char [newlistrecord.size()+1];
                strcpy (newrecord, newlistrecord.c_str());
  
               if ( s.OutputClientAvail() == 1) //check  if output client is available
               { 
                int ret = s.SendBytes(newrecord,strlen(newrecord));                        
                if ( ret &lt; 0)
                 { 
                  log1.AddLogFormatFatal("Nice Send Thread : Nice Client Disconnected");    
                   --connected;
                    return;
                 }                                              
              }
              else
              {
               log1.AddLogFormatFatal("Nice Send Thread : Nice Client Timedout..connection closed");    
               --connected; //if output client not available disconnect after a timeout
               return;
              }                                        
            }    
  
         }  
        }
  
      //  Sleep(100); if we include sleep here CPU usage is less..but to send data real time I need to remove this sleep.
  If I remove Sleep()...CPU usage goes very high while sending data to out put port.          
    }//End of while loop
  </code></pre>
  
  <p>Any possible ways to maintain real time data transfer and reduce CPU usage..please suggest.</p>