一段socket接收判断函数。保证能够接收完成

void __fastcall TServerConnect::TCPClientPort_ReadBuffer(TObject *Sender, TCustomWinSocket *Socket)
{


     int count = Socket->ReceiveLength();
    // Socket->
     Socket->ReceiveBuf(&RecBuffer[Recount],count);
     Recount+=count;
     while (Recount>0)
     {
          if (Recount>6)
          {
             int Requsetlen=RecBuffer[4]+RecBuffer[5]*256+6;;
             if (RecBuffer[0]==0xCB&&RecBuffer[1]==0x09&&RecBuffer[2]==0xCB&&RecBuffer[3]==0x09&&Requsetlen<=Recount)
             {


                         memset(ProBuffer,0,sizeof(ProBuffer));//接收数组

                         memcpy(&ProBuffer[0],RecBuffer,Requsetlen);
                         memcpy(RecBuffer,&RecBuffer[Requsetlen],sizeof(RecBuffer)-Requsetlen);
                         Recount=Recount-Requsetlen;
                         if (ProBuffer[7]*256+ProBuffer[6]==2)//电参数
                         {

                                int index=ProBuffer[11]*256*256*256+ProBuffer[10]*256*256+ProBuffer[9]*256+ProBuffer[8];  //判断 节点ID
                                for (int i=0;iCount;i++)
                                {
                                     TDeviceData*tempdata=(TDeviceData*)DeviceDataList->Items[i];
                                     if (index==tempdata->DeviceIndex)
                                     {
                                           memcpy(tempdata,&ProBuffer[18],sizeof(TDeviceData));
                                           tempdata->ByteData[4]=0x80;
                                           tempdata->DoubleData[8]=0x80;
                                           tempdata->ByteData[3]=96;//0x80;
                                           break ;
                                     }

                                }

                         }
                         if (ProBuffer[7]*256+ProBuffer[6]==5)//事件
                         {
                                //S_SoeInfo
                                //int index=ProBuffer[11]*256*256*256+ProBuffer[10]*256*256+ProBuffer[9]*256+ProBuffer[8];//判断 节点ID

                                 SOE_CommINFO *tempdata=new SOE_CommINFO();
                                 memcpy(tempdata,&ProBuffer[18],sizeof(SOE_CommINFO));
                                 NewSoeDataList->Add(tempdata);
                                 break ;




                         }











                     
             }
          }
          else
          {

              return;
          }
     }

}

你可能感兴趣的:(C++,builder)