Увеличение ЦП TCP C ++ с течением времени (часов) - PullRequest
0 голосов
/ 11 марта 2020

У меня есть следующий код. Он обслуживает около 10 ручных сканеров штрих-кода. Все работает нормально, за исключением увеличения загрузки процессора с течением времени в часах, а иногда и днях. Я перепроверил все переменные и проверил с помощью Process Explorer. Нет утечки памяти, а потоки / TCP-соединения ведут себя как положено. Пожалуйста помоги. Спасибо.

#include "sql_serv.h"
#include <stdio.h>
#include <iostream>
#include "mingw.thread.h"
#include "str_functions.h"


void lf_client( SOCKET client)
{
     string LF ="\n\r";
     send (client,LF.c_str(),LF.size(),0);
}

string CONN_STR;
string CONN_STR_READ_ONLY;


 int overCPU( )
 {
   while(true)
     {
          Sleep(10*60000); // 10*60000  10 minutes

         if(SQL_SERV_SEL( "EXEC [CPU_OVER_LIMIT_RST_GET]",CONN_STR_READ_ONLY) == "RST")
         {
            INS_UPD_DEL_SQL_SERV("EXEC [EXEC_OVER_CPU_LOG];",CONN_STR);
            Sleep(1500);
            WSACleanup();
            Sleep(1500);
            exit(0);
         }

     }
 }

 /* send(), recv(), closesocket() */

DWORD WINAPI receive_cmds(LPVOID lpParam)
{
    SOCKET current_client = (SOCKET)lpParam;

   bool list_ord = false;
  string ClearScreen= "[2J[H[A";
  string MENU_ALEX = "F2 Acasa\n\rF3 Log Off\n\r1 Comenzi\n\rL Listare Com\n\r2 Receptie\n\r3 UrcareRaft\n\r4 Cobor_Raft \n\r5 Consolid.Pal\n\r6 Schimb.Cant(S)\n\r7 Info Eticheta\n\r8 (S)/Master\n\r0 Inventar _";
  string builMasterMenu ="\r\n1=CloseMPal\r\n2-CancelMPal";
  string Beep2 ="";
  string Beep3 ="";
//******************** others**************


 // bool SN_STRICT =false;
  bool subRECEP =false;
  bool subSPLIT =false;
  bool UrcareRaft=false;
  bool Cobor_Raft=false;
  string  Cobor_Raft_MLL="";
  bool SN_CHANGE=false;
  bool INFO_ETICHETA= false;
  bool S_Per_Master= false;
  string S_Per_Master_MLL ="";

  //******************** others **************

 // bool SERIAL_KNOW =false;
  bool HOME_MENU= true;

  char recvbuf;
      // char buf[100] ;
      //  char sendData[256];
  int res;


    string check_conn= SQL_SERV_CHECK_CONNECTION(CONN_STR_READ_ONLY) ;
    send (current_client,ClearScreen.c_str(),ClearScreen.size(),0);
    string snd = check_conn + "\r\n*ServerV8*\r\nstd::thread\r\nCPU >__ RST\r\nSCAN_BADGE:\r\n";
    send (current_client, snd.c_str(), snd.size(),0); snd.clear();

    check_conn.clear();
    tempData.clear();


while(true) // LOOPER BEGIN
  {
    res = recv(current_client, &recvbuf, 1, 0);
    Sleep(15);
    //#Client Discon**********************
    if(res == 0)
    {
        if(ORDER_NR != "")
        {
        INS_UPD_DEL_SQL_SERV( "UPDATE [ORDERS_SCAN_STAT] SET STATUS_ASSI_FREE='DISC' WHERE ORD_NR =" + ORDER_NR,
                                 CONN_STR);

        }
      closesocket(current_client);
      return 0;
    }

    if(recvbuf != 0) {tempData+=recvbuf;}

    // Restart App
    if (ReplaceString(tempData,"\n","")=="X000X0")
        {
             INS_UPD_DEL_SQL_SERV("INSERT INTO [LOG_CPU_OVER_RSR] VALUES(CURRENT_TIMESTAMP ,'MANU_RS');",CONN_STR);

            if(ORDER_NR != "")
                {
                    INS_UPD_DEL_SQL_SERV( "UPDATE [ORDERS_SCAN_STAT] SET STATUS_ASSI_FREE='DISC' WHERE ORD_NR =" + ORDER_NR,
                                         CONN_STR);
                }

            exit(0);
        }

    //! NUM FOR NORMAL PROC NEGIN
    if(tempData.length()>0 && HOME_MENU==true && tempData.length()<3 )
    {
      string menu_access =   ReplaceString( tempData,"\n","");
      menu_access =ReplaceString( menu_access,"\r","");
        //INVENTAR
        if (menu_access=="0")
         {
            Inventar=true;
            Inventar_LOC.clear();
            Inventar_MLL.clear();
            Inventar_SN.clear();
            ser_inDB=false;
            Inventar_COMP.clear();
            Inventar_QUNA.clear();

            menu_access.clear();
            send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
            string snd ="F2 Acasa:\r\nINVENTAR\r\n1 NEXT\r\nSCAN_LOC:\r\n";
            send (current_client, snd.c_str(), snd.size(),0); snd.clear();

            tempData.clear();
            HOME_MENU=false;
         }
         //if (menu_access=="0") INVENTAR END
      //SC_COMANDA
      if (menu_access=="1")
        {
          ORDER_SCAN =true;
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\nSC_COMANDA\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();
          menu_access.clear();
          tempData.clear();
          HOME_MENU=false;
        }
        //if (menu_access=="1")  SC_COMANDA  END
        //RECEPTIE
        if (menu_access=="2")
        {
          subRECEP=true;
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\nRECEPTIE\r\nSC_DOC_RECEPT\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();
          menu_access.clear();
          tempData.clear();
          HOME_MENU=false;
        }
        //if (menu_access=="2")  RECEPTIE END
        //URCARE LA LOC
        if (menu_access=="3")
        {
          UrcareRaft =true;
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string free_loc= SQL_SERV_SEL( "SELECT top 2 Adresa + ',' AS 'data()'  FROM FREE_LOC FOR XML PATH('')",
          CONN_STR_READ_ONLY);
          string snd = "F2 Acasa:\r\nURCARE LA LOC\r\n" + free_loc + "\r\nSCAN_LOC:\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();

          free_loc.clear();
          tempData.clear();
          menu_access.clear();
          HOME_MENU=false;
        }
        // if (menu_access=="3")  URCARE LA LOC  END
        //COBOR_PAL
        if (menu_access=="4")
        {
          Cobor_Raft =true;
          Cobor_Raft_MLL.clear();
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\nCOBOR_PAL\r\nSCAN_MLL:\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();

          tempData.clear();
          menu_access.clear();
          HOME_MENU=false;
        }
        //if (menu_access=="4") COBOR_PAL END
        //TRANSF_CUTII  Consolid.Pal

        if (menu_access=="5")
        {
          subSPLIT = true;
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\nTRANSF_CUTII\r\nSC_PALET_SURSA\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();
          tempData.clear();
          menu_access.clear();
          HOME_MENU=false;
        }
        //if (menu_access=="5") TRANSF_CUTII END
        //(S)CHIMB CANT
        if (menu_access=="6")
        {
          SN_CHANGE=true;
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\n(S)CHIMB CANT\r\nSCAN_SN:\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();
          tempData.clear();
          menu_access.clear();

          HOME_MENU=false;
        }
        //if (menu_access=="6") (S)CHIMB CANT END
        //INFO_ETICH
        if (menu_access=="7")
         {
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\nINFO_ETICH\r\nSCAN_ETICH\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();

          INFO_ETICHETA = true;
          INFO_ETICHETA_SC_SOURCE="";
          tempData.clear();
          menu_access.clear();

          HOME_MENU=false;
         }
         //if (menu_access=="7") INFO_ETICH END
        //"Nr(S)/Master"
        if (menu_access=="8")
         {
          S_Per_Master = true;
          send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
          string snd = "F2 Acasa:\r\nNr(S)/Master\r\nSCAN_ML\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();
          S_Per_Master_MLL ="";
          tempData.clear();
          menu_access.clear();
          HOME_MENU=false;
         }
         //if (menu_access=="8") "Nr(S)/Master" END

    }
    //! NUM FOR NORMAL PROC END

    //! F KEYS BEGIN
    if(tempData.length()>2)
     {
      //F3 exit
      if ( tempData.length()>2 && tempData.substr(tempData.length()-3,3)=="OR")
      {

        ClearScreen.clear() ;
        MENU_ALEX .clear() ;
        builMasterMenu.clear() ;
        Beep2.clear();
        Beep3.clear();

        ORDER_SCAN =false;

        subRECEP =false;
        subSPLIT =false;
        UrcareRaft=false;
        Cobor_Raft=false;
        Cobor_Raft_MLL.clear();
        SN_CHANGE=false;
        S_Per_Master= false;


        S_Per_Master_MLL.clear();
        FULL_PAL_ORD = false;

        INFO_ETICHETA= false;
        INFO_ETICHETA_SC_SOURCE.clear();

        ORDER_SCAN_ML_FULL.clear();


        Profile .clear();
        COMP_ALEX .clear();
        Q_ALEX .clear();
        USER_ALEX.clear();
        LOC_ALEX.clear();
        MASTER_NR.clear();
        DN_TO .clear();

        subRECEP_CNTSN.clear();
        SERIAL_ALEX_RC.clear();


        subMLLOC_MASTER_LABEL.clear();
        subMLLOC_LOCAT.clear();
        subMLLOC_LOCAT_conf.clear();

        SPIT_SOURCE_PAL .clear();
        SPIT_DEST_PAL .clear();
        SPIT_DEST_PAL_LOCAT.clear();
        SPIT_SOURCE_PAL_LOCAT.clear();


        if(ORDER_NR != "")
        {
            INS_UPD_DEL_SQL_SERV( "UPDATE [ORDERS_SCAN_STAT] SET STATUS_ASSI_FREE='DISC' WHERE ORD_NR =" + ORDER_NR,
                                 CONN_STR);
        }
        ORDER_NR .clear();

        ORDER_SCAN_SN_DELETER .clear();
        ORDER_SCAN_MLL .clear();
        id_RoW_ORDER .clear();
        NR_SN_ORD.clear();
        NR_SN_MLL.clear();
        ORDER_MAT_PER_MLL.clear();
        //TO_TPRD = "";

        SN_CHANGE_SER .clear();
        SN_CHANGE_QUAN .clear();

        Sn_INI_COUNT .clear();
        //SERIAL_KNOW =false;

        tempData.clear();


        send(current_client,"Good Bye\n",9,0);
        Sleep(1000);

        closesocket(current_client);
        return 0;
      }
     //F3 exit END

      //F2 Home
      if (tempData.length()>2 && tempData.substr(tempData.length()-3,3)=="OQ")
      {

/*
        Inventar=false;
        Inventar_LOC.clear();
        Inventar_MLL.clear();
        Inventar_SN.clear();
        ser_inDB=false;
        Inventar_COMP.clear();
        Inventar_QUNA.clear();
*/
        HOME_MENU =true;

        FULL_PAL_ORD = false;
        ORDER_SCAN =false;
        subRECEP =false;
        subSPLIT =false;
        UrcareRaft=false;
        Cobor_Raft=false;
        Cobor_Raft_MLL.clear();
        SN_CHANGE=false;
        S_Per_Master= false;


        S_Per_Master_MLL.clear();

        INFO_ETICHETA= false;
        INFO_ETICHETA_SC_SOURCE.clear();

        ORDER_SCAN_ML_FULL.clear();

        Profile .clear();
        COMP_ALEX .clear();
        Q_ALEX .clear();
        LOC_ALEX.clear();
        MASTER_NR.clear();
        DN_TO .clear();

        subRECEP_CNTSN.clear();
        SERIAL_ALEX_RC.clear();

        subMLLOC_MASTER_LABEL.clear();
        subMLLOC_LOCAT.clear();
        subMLLOC_LOCAT_conf.clear();

        SPIT_SOURCE_PAL .clear();
        SPIT_DEST_PAL .clear();
        SPIT_DEST_PAL_LOCAT.clear();
        SPIT_SOURCE_PAL_LOCAT.clear();

        if(ORDER_NR != "")
        {
            INS_UPD_DEL_SQL_SERV( "UPDATE [ORDERS_SCAN_STAT] SET STATUS_ASSI_FREE='DISC' WHERE ORD_NR =" + ORDER_NR,
                                 CONN_STR);
        }
        ORDER_NR.clear();
        ORDER_SCAN_SN_DELETER .clear();
        ORDER_SCAN_MLL .clear();
        id_RoW_ORDER .clear();
        NR_SN_ORD.clear();
        NR_SN_MLL.clear();
        ORDER_MAT_PER_MLL.clear();
        //TO_TPRD.clear();

        SN_CHANGE_SER .clear();
        SN_CHANGE_QUAN .clear();

        Sn_INI_COUNT .clear();
        //SERIAL_KNOW =false;


        tempData.clear();
        send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
        string snd = MENU_ALEX + "\r\n";
        send (current_client, snd.c_str(), snd.size(),0); snd.clear();

        if (USER_ALEX=="")
        {
            send (current_client,ClearScreen.c_str(),ClearScreen.size(),0);

            string snd = "SCAN_BADGE:\r\n";
            send (current_client, snd.c_str(), snd.size(),0); snd.clear();

        }
      }
      //F2 Home END
     }
     // if(tempData.length()>2)END
     //! F KEYS END
    // ENTER Key CR Action DO BEGIN
    if(recvbuf=='\r')
    {

        ReplaceStringInPlace(tempData, "\n", "");
        ReplaceStringInPlace(tempData, "\r", "");
        ReplaceStringInPlace(tempData, "[A", "");
        ReplaceStringInPlace(tempData, "[B", "");
        ReplaceStringInPlace(tempData, "[C", "");
        ReplaceStringInPlace(tempData, "[D", "");

        // SEND LOGIN
        if ( USER_ALEX=="" && tempData.length()==0)
        {
          send (current_client,ClearScreen.c_str(),ClearScreen.size(),0);
          string snd = "ServerV8*\r\nstd::thread\r\nCPU >__ RST\r\nSCAN_BADGE:\r\n";
          send (current_client, snd.c_str(), snd.size(),0); snd.clear();
        }
        //if ( USER_ALEX=="" && tempData.length()==0) END

         //GET LOGIN
        if (USER_ALEX=="" && tempData.length()>0)
        {
           Profile= SQL_SERV_SEL("SELECT [role] FROM usersrole WHERE [USER]='" + tempData  + "';",
                          CONN_STR_READ_ONLY);
            if (Profile=="INV2")
            {
              USER_ALEX =tempData;
              string u_ex = SQL_SERV_SEL( "SELECT [FULLNAME] FROM  WHOISWHO WHERE [SHORT_ID] ='" + USER_ALEX + "';",CONN_STR_READ_ONLY);
              send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
              string snd = "Welcome::\r\n" + u_ex + "\r\n" + MENU_ALEX + "\r\n";
              send (current_client, snd.c_str(), snd.size(),0); snd.clear();
              u_ex.clear();
            }
            Profile.clear();
            tempData.clear();
          }
            //if (USER_ALEX=="" && tempData.length()>0) END

        //!MAIN PROFILE
        if (USER_ALEX!="")
        {   
//****************************OTHERS BEFORE***********************
               //! UrcareRaft BEGIN
            if (UrcareRaft==true )
            {
              // REQ subMLLOC_LOCAT BEGIN
                if (subMLLOC_LOCAT==""  && tempData.length()==0)
                {
                  send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                  string snd = "F2 Acasa:\r\nURCARE LA LOC\r\nSCAN_LOC:\r\n";
                  send (current_client, snd.c_str(), snd.size(),0); snd.clear();
                  tempData.clear();
                }
              // REQ subMLLOC_LOCAT END

              // GET CHECK subMLLOC_LOCAT BEGIN
              if (subMLLOC_LOCAT==""  && tempData.length()>0)
                {
                    string new_App ="";
                    new_App+=" SELECT  [LOC_ALEX_in] + CASE WHEN  COUNT (DISTINCT MLL)=0 THEN '' ELSE '@WARN' END ";
                    new_App+=" + CASE WHEN  COUNT (DISTINCT MLL) < (SELECT MAXNr FROM MAXNrOfMLLPerLOCAT)  THEN '' ELSE 'KO' END as MLLP ";
                    new_App+=" FROM  ALEX_EXT_LOCATII  LEFT JOIN ALEX_RECEP ON LOC_ALEX_in= LOCAT ";
                    new_App+=" WHERE [LOC_ALEX_in]='"+tempData+"' GROUP BY  LOC_ALEX_in ";

                    subMLLOC_LOCAT =SQL_SERV_SEL(new_App,CONN_STR_READ_ONLY) ;

                    new_App.clear();
                    std::size_t ERROR_IS = subMLLOC_LOCAT.find_first_of("KO");

                        if (static_cast<int>(ERROR_IS)>0)
                        {
                          subMLLOC_LOCAT ="MAXNRPAL=1";
                        }

                    std::size_t WARN_IS = subMLLOC_LOCAT.find_first_of("@");
                      if (static_cast<int>(WARN_IS)>0)
                      {
                        subMLLOC_LOCAT = subMLLOC_LOCAT.substr(0,WARN_IS);
                        send (current_client,Beep3.c_str(),Beep3.size(),0);
                      }
                    // Error
                    if (subMLLOC_LOCAT!=tempData)
                    {
                        send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                        send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
                        send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);
                        send (current_client,"SCAN_LOC:",9,0);lf_client(current_client);
                        send (current_client,subMLLOC_LOCAT.c_str(),subMLLOC_LOCAT.size(),0);
                        lf_client(current_client);
                        send (current_client,"Scan LOC", 8,0);
                        lf_client(current_client);
                        send (current_client,Beep2.c_str(), Beep2.size(),0);

                        tempData.clear();
                        subMLLOC_LOCAT.clear();
                    }
                      tempData.clear();
                }
                  // GET CHECK subMLLOC_LOCAT END

                  // REQ subMLLOC_MASTER_LABEL BEGIN
                    if (subMLLOC_LOCAT!="" && subMLLOC_MASTER_LABEL==""  && tempData.length()==0)
                        {
                            send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                            send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
                            send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);
                            send (current_client,subMLLOC_LOCAT.c_str(),subMLLOC_LOCAT.size(),0);

                            lf_client(current_client);
                            send (current_client,"SCAN_MLL:",9,0);
                            lf_client(current_client);
                        }
                  // REQ subMLLOC_MASTER_LABEL END

                // GET CHECK subMLLOC_MASTER_LABEL BEGIN L+
                if (subMLLOC_LOCAT!="" && subMLLOC_MASTER_LABEL==""  && tempData.length()>0)
                {
                      string check_mll ="";
                      check_mll="SELECT TOP 1 DN_TO+'@RECTP_NO_COMPL'  FROM PENDING_RCP ";
                      check_mll+=" WHERE DN_TO =";
                      check_mll+="(SELECT TOP 1 DN_TO FROM ALEX_RECEP WHERE MLL ='"+tempData+"')";

                      string look_valid =SQL_SERV_SEL(check_mll,CONN_STR_READ_ONLY);
                      // Error
                      if (look_valid!="")
                      {
                          subMLLOC_MASTER_LABEL=look_valid;
                      }
                        if (look_valid=="")  // OK check if mll in alex
                        {

                          subMLLOC_MASTER_LABEL =SQL_SERV_SEL("SELECT TOP 1 MLL FROM ALEX_RECEP WHERE MLL='" + tempData + "';"
                                                              ,CONN_STR_READ_ONLY);

                          string on_loc = SQL_SERV_SEL( "SELECT TOP 1  LOCAT FROM ALEX_RECEP WHERE MLL='" + tempData + "';",CONN_STR_READ_ONLY);

                            if(on_loc!="0") subMLLOC_MASTER_LABEL ="@DEJA IN LOCATIA@|" +on_loc+"|";
                            if(on_loc=="") subMLLOC_MASTER_LABEL ="@ML NOK";

                          on_loc.clear();
                        }

                      check_mll.clear();
                      look_valid.clear();
                          // Error
                      if (subMLLOC_MASTER_LABEL!=tempData )
                      {
                            send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                            send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
                            send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);

                            send (current_client,"ML_NOK:",7,0);

                            ReplaceStringInPlace(subMLLOC_MASTER_LABEL,"@","\r\n");
                            send (current_client,subMLLOC_MASTER_LABEL.c_str(),subMLLOC_MASTER_LABEL.size(),0);
                            lf_client(current_client);
                            lf_client(current_client);
                            send (current_client,"SCAN_MLL:",9,0);
                            lf_client(current_client);
                            subMLLOC_MASTER_LABEL.clear();

                            send (current_client,Beep2.c_str(), Beep2.size(),0);
                      }
                          tempData.clear();
                }

              // GET CHECK subMLLOC_MASTER_LABEL END

                // REQ subMLLOC_LOCAT_conf BEGIN
              if (subMLLOC_LOCAT!="" && subMLLOC_MASTER_LABEL!="" && subMLLOC_LOCAT_conf==""  && tempData.length()==0)
            {
              send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
              send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
              send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);
              send (current_client,subMLLOC_LOCAT.c_str(),subMLLOC_LOCAT.size(),0);
              send (current_client,"\r\nConfirm LOC:\r\n",16,0);
              tempData.clear();
            }
                // REQ subMLLOC_LOCAT_conf END

              // GET subMLLOC_LOCAT_conf BEGIN
              if (subMLLOC_LOCAT!="" && subMLLOC_MASTER_LABEL!="" && subMLLOC_LOCAT_conf==""  && tempData.length()>0)
              {
                  subMLLOC_LOCAT_conf =tempData;
                  // Error
                  if (subMLLOC_LOCAT_conf!= subMLLOC_LOCAT )
                  {
                    send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                    send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
                    send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);
                    send (current_client,subMLLOC_LOCAT.c_str(),subMLLOC_LOCAT.size(),0);
                    lf_client(current_client);
                    send (current_client,"\r\nSN_NO_MACH:\r\n",14,0);
                    send (current_client,"\r\nConfirm LOC:\r\n",16,0);
                    subMLLOC_LOCAT_conf.clear();
                    send (current_client,Beep2.c_str(), Beep2.size(),0);
                  }
                    tempData.clear();
              }
              // GET subMLLOC_LOCAT_conf END

            // FINISH  MOVE MASTER TO LOCATIONS  L+ BEGIN
            if (subMLLOC_LOCAT!="" && subMLLOC_MASTER_LABEL!="" && subMLLOC_LOCAT_conf!=""  && tempData.length()==0)
                  {
                    string check_UP =INS_UPD_DEL_SQL_SERV( "UPDATE ALEX_RECEP SET [LOCAT]='" + subMLLOC_LOCAT + "' WHERE [MLL]='"+ subMLLOC_MASTER_LABEL + "';" ,
                    CONN_STR);
                    //TRASAB_V3
                    //string TRASAB_V3_URCA = "UPDATE ALEX_RECEP SET [LOCAT]= '" + subMLLOC_LOCAT + "' WHERE [MLL]= '" + subMLLOC_MASTER_LABEL +"';\r\n";
                    string TRASAB_V3_URCA = "INSERT INTO TRASAB_V3_URCA ([M_DATE], [USER_], [SN], [MLL], [LOCAT]) \r\n";
                    TRASAB_V3_URCA += " SELECT CURRENT_TIMESTAMP, '" + USER_ALEX + "', SN, '" + subMLLOC_MASTER_LABEL  + "', '" + subMLLOC_LOCAT  + "'";
                    TRASAB_V3_URCA += " FROM ALEX_RECEP WHERE [MLL] = '"  + subMLLOC_MASTER_LABEL + "'" ;
                    //TRASAB_V3 END

                      // Error
                      if ( check_UP !="OK")
                          {
                              send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                              send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
                              send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);
                              send (current_client,check_UP.c_str(),check_UP.size(),0);
                              send (current_client,"\r\nRetry\r\n",9,0);
                              send (current_client,"Scan LOC",8,0);
                                lf_client(current_client);
                              subMLLOC_LOCAT_conf.clear();

                              send (current_client,Beep2.c_str(), Beep2.size(),0);
                          }
                          // ok done
                      else {
                              string t_conf = subMLLOC_MASTER_LABEL  + " DONE\r\n";
                              send (current_client,ClearScreen.c_str(), ClearScreen.size(),0);
                              send (current_client,"F2 Acasa:",8,0);lf_client(current_client);
                              send (current_client,"URCARE LA LOC",13,0);lf_client(current_client);
                                string free_loc= SQL_SERV_SEL( "SELECT top 2 Adresa + ',' AS 'data()'  FROM FREE_LOC order by SUBSTRING(Adresa,1,3)  FOR XML PATH('')",CONN_STR_READ_ONLY);
                              send (current_client,free_loc.c_str() ,free_loc.size(),0);lf_client(current_client);
                              free_loc.clear();

                              send (current_client,subMLLOC_MASTER_LABEL.c_str(),subMLLOC_MASTER_LABEL.size(),0);
                              send (current_client,check_UP.c_str(),check_UP.size(),0);
                              lf_client(current_client);
                              send (current_client,"Scan LOC",8,0);
                                    lf_client(current_client);
                              t_conf.clear();

                              subMLLOC_LOCAT.clear();
                              subMLLOC_MASTER_LABEL.clear();
                              subMLLOC_LOCAT_conf.clear();

                            INS_UPD_DEL_SQL_SERV(TRASAB_V3_URCA,CONN_STR);
                      }
                          TRASAB_V3_URCA.clear();
                        check_UP.clear();
                        tempData.clear();
                  }

            // FINISH  MOVE MASTER TO LOCATIONS  L+ END
            }
            //! UrcareRaft END

    //****************************AND SO ON ***********************
     }
        //! END MAIN PROFILE
 tempData.clear();
 }
    //if(recvbuf=='\r') END
     }
 //while(true) // LOOPER BEGIN
  } //DWORD WINAPI receive_cmdsPLUS(LPVOID lpParam) END




int main()
{
    printf("Starting up multi-threaded TCP server \r\n");
    FILE *fileStream_SQL; char fileText_SQL [240];
    fileStream_SQL = fopen ("PATH_sql.txt", "r");
    fgets (fileText_SQL, 240, fileStream_SQL);
    CONN_STR = fileText_SQL;
    CONN_STR_READ_ONLY =ReplaceString(CONN_STR,"Uid=sa;","Uid=alex_ro;");
    fileText_SQL[0] ='\0';
    fclose(fileStream_SQL);

 // our masterSocket(socket that listens for connections)
 SOCKET sock;

 // for our thread
 //DWORD thread;

 WSADATA wsaData;
 sockaddr_in server;

 // start winsock
 int ret = WSAStartup(0x101,&wsaData); // use highest version of winsock avalible

 if(ret != 0)
 {
    return 0;
 }

 // fill in winsock struct ...
 server.sin_family=AF_INET;
 server.sin_addr.s_addr=INADDR_ANY;
 server.sin_port=htons(23); // listen on telnet port 23

 // create our socket
 sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

 if(sock == INVALID_SOCKET)
 {
    return 0;
 }

 // bind our socket to a port(port 23)
 if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 )
 {
    return 0;
 }

 // listen for a connection
 if(listen(sock,5) != 0)
 {
    return 0;
 }

 printf("TCP server sin_port 23 \r\n");

 // socket that we snedzrecv data on
 SOCKET client;

 sockaddr_in from;
 int fromlen = sizeof(from);


 std::thread th_overCPU(overCPU);
th_overCPU.detach();

 // loop forever
  while ((client = accept(sock, (struct sockaddr*)&from,&fromlen))!= INVALID_SOCKET)
    {
    Sleep(250);
    std::thread th_client(receive_cmds,(LPVOID)client);
    th_client.detach();

 }
 // shutdown winsock
 closesocket(sock);
 WSACleanup();

 // exit
 return 0;
}

1 Ответ

0 голосов
/ 11 марта 2020

Скелет приложения

#include "sql_serv.h"
#include <stdio.h>
#include <iostream>
#include "mingw.thread.h"
#include "str_functions.h"

DWORD WINAPI receive_cmds(LPVOID lpParam);

int main()
{
 // our masterSocket(socket that listens for connections)
 SOCKET sock;
 WSADATA wsaData;
 sockaddr_in server;
 // start winsock
 int ret = WSAStartup(0x101,&wsaData); // use highest version of winsock avalible
 if(ret != 0)
 {
    return 0;
 }

 // fill in winsock struct ...
 server.sin_family=AF_INET;
 server.sin_addr.s_addr=INADDR_ANY;
 server.sin_port=htons(23); // listen on telnet port 23
 // create our socket
 sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
 if(sock == INVALID_SOCKET)
 {
    return 0;
 }
 // bind our socket to a port(port 23)
 if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 )
 {
    return 0;
 }
 // listen for a connection
 if(listen(sock,5) != 0)
 {
    return 0;
 }
 printf("TCP server sin_port 23 \r\n");
// socket that we sned recv data on
 SOCKET client;
 sockaddr_in from;
 int fromlen = sizeof(from);
 // loop forever
  while ((client = accept(sock, (struct sockaddr*)&from,&fromlen))!= INVALID_SOCKET)
    {
    Sleep(250);
    std::thread th_client(receive_cmds,(LPVOID)client);
    th_client.detach();
 }
 // shutdown winsock
 closesocket(sock);
 WSACleanup();
 // exit
 return 0;
}

DWORD WINAPI receive_cmds(LPVOID lpParam)
{
    int res;
    char recvbuf;
    SOCKET current_client = (SOCKET)lpParam;
    string tempData;
    while(true) // LOOPER BEGIN
      {
        res = recv(current_client, &recvbuf, 1, 0);
        Sleep(15);
        //#Client Discon**********************
        if(res == 0)
        {
          closesocket(current_client);
          return 0;
        }

        if(recvbuf != 0) {tempData+=recvbuf;}
        // * A long code based on tempData content
      }
}
``
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...