daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************************
  2.    Function: HelpMenu
  3.          In:
  4.         Out:
  5.      Return:
  6. Description:
  7. *******************************************************************************/
  8. void HelpMenu(int iSockID)
  9. {
  10.   iSendToClient(iSockID,"*** HELP MENU ***\n");
  11.   iSendToClient(iSockID,"[getdate <ddmmyy>] meaning: /n return type, valstr and valnum for line with max (hh:mm:ss) for date <ddmmyy>\n");
  12.   iSendToClient(iSockID,"[count] meaning: /n return number of record for every different type in table test_marin\n");
  13. }
  14. /*******************************************************************************
  15.    Function: iGetDate
  16.          In:
  17.         Out:
  18.      Return:
  19. Description:
  20. *******************************************************************************/
  21. int getDate(int iSockID, char *pstrDate)
  22. {
  23.   EXEC SQL BEGIN DECLARE SECTION;
  24.   SQL20 qstrValdateOut,qstrValdateIn;
  25.   EXEC SQL END DECLARE SECTION;
  26.  
  27.   memcpy(qstrValdateIn,pstrDate,(strlen(pstrDate)+1));
  28.  
  29.   EXEC SQL WHENEVER SQLERROR DO return (sqlError (ERR_SQL_ERROR, "sql error: [getDate] select\n"));
  30.   EXEC SQL WHENEVER NOT FOUND DO return (sqlError (ERR_NOT_FOUND, "not found error: [getDate] select.\n"));
  31.  
  32.   EXEC SQL SELECT TO_CHAR (MAX(valdat)) AS qstrValdateOut
  33.              FROM test_edu
  34.             WHERE valdat = qstrValdateIn;
  35.  
  36.   memcpy(pstrDate,qstrValdateOut,(strlen(qstrValdateOut)+1));
  37.   iSendToClient(iSockID, pstrDate);
  38. }
  39. /*******************************************************************************
  40.    Function: iCount
  41.          In:
  42.         Out:
  43.      Return:
  44. Description:
  45. *******************************************************************************/
  46. int iCount(int iSockID)
  47. {
  48.   EXEC SQL BEGIN DECLARE SECTION;
  49.   SQL20 qstrCount;
  50.   EXEC SQL END DECLARE SECTION;
  51.  
  52.   char *pstrBuf;
  53.  
  54.   EXEC SQL WHENEVER SQLERROR DO return (sqlError (ERR_SQL_ERROR, "sql error: [iCount] declare countCursor\n"));
  55.   EXEC SQL WHENEVER NOT FOUND DO return (sqlError (ERR_NOT_FOUND, "not found error: [iCount] declare countCursor.\n"));
  56.  
  57.   EXEC SQL DECLARE countCursor CURSOR FOR
  58.                                    SELECT type,
  59.                                           COUNT(*)
  60.                                      FROM test_marin
  61.                                  GROUP BY type
  62.                                  ORDER BY type;
  63.  
  64.   EXEC SQL WHENEVER SQLERROR DO return (sqlError (ERR_SQL_ERROR, "sql error: [iCount] open countCursor\n"));
  65.   EXEC SQL WHENEVER NOT FOUND DO return (sqlError (ERR_NOT_FOUND, "not found error: [iCount] open countCursor.\n"));
  66.  
  67.   EXEC SQL OPEN countCursor;
  68.  
  69.   for (;;)
  70.   {  
  71.     EXEC SQL WHENEVER SQLERROR DO return (sqlError (ERR_SQL_ERROR, "sql error: [iCount] fetch cursor.\n"));
  72.     EXEC SQL WHENEVER NOT FOUND DO break;
  73.      
  74.     EXEC SQL FETCH countCursor
  75.               INTO qstrCount;
  76.  
  77.   memcpy(pstrBuf,qstrCount,strlen(qstrCount));
  78.                    
  79.   iSendToClient(iSockID,pstrBuf);
  80.   }
  81.  
  82.   EXEC SQL WHENEVER SQLERROR DO return (sqlError (ERR_SQL_ERROR, "sql error: [iCount] close countCursor\n"));
  83.   EXEC SQL WHENEVER NOT FOUND DO return (sqlError (ERR_NOT_FOUND, "not found error: [iCount] close countCursor.\n"));
  84.  
  85.   EXEC SQL CLOSE countCursor;
  86.  
  87.   return 0;  
  88. }
  89. /*******************************************************************************
  90.    Function: iSendToClient
  91.          In:
  92.         Out:
  93.      Return: 0 on succes
  94.              1 on fail
  95. Description: send data
  96. *******************************************************************************/
  97. int iSendToClient(int iSockID, char *pstrBuffer)
  98. {
  99.   if ((send (iSockID, pstrBuffer, strlen (pstrBuffer) ,0)) == -1)
  100.   {
  101.     logs("error: [iSendToClient] send fail.\n");
  102.     return 1;
  103.   }
  104.   logs("succes: [iSendToClient] send done.\n");
  105.   return 0;
  106. }
  107. /*******************************************************************************
  108.    Function: iCheckInputSocket
  109.          In:
  110.         Out:
  111.      Return:
  112. Description: checks arguments
  113. *******************************************************************************/
  114. int iCheckInputSocket(char *pstrBuffer, char *pstrDate)
  115. {
  116.   int iFlag;
  117.   char strFirstArg[MAX_SOCKET_BUF_SIZE], strSecondArg[MAX_SOCKET_BUF_SIZE], strThirdArg[MAX_SOCKET_BUF_SIZE];
  118.  
  119.   iFlag = sscanf(pstrBuffer,"%s %s %s",strFirstArg,strSecondArg,strThirdArg);
  120.   logs("debug: [iCheckInput]iRes from sscanf: %d\n", iRes);
  121.  
  122.   if(iFlag == 3)
  123.   {
  124.     logs("[iCheckInputSocket] more than two arguments.\n");
  125.     return ERR_SOCKET_ARGUMENTS_NUM;
  126.   }
  127.  
  128.   if (iFlag == 2)
  129.   {
  130.     if (!(strcmp(strFirstArg,"getdate")))
  131.     {
  132.       if (iCheckDate(strSecondArg,"ddmmyyyy")==1)
  133.       {
  134.         logs("[iCheckInput]Client choose: getdate <ddmmyyyy>.\n");
  135.         memcpy(pstrDate,strSecondArg,strlen(strSecondArg));
  136.         return SOCKET_INPUT_GETDATE;
  137.       }
  138.     }
  139.   }
  140.  
  141.   if (iFlag==1)
  142.   {
  143.     if (!(strcmp(pstrBuffer,"quit")))
  144.     {
  145.       logs("[iCheckInput]Client choose: quit.\n");
  146.       return SOCKET_INPUT_QUIT;
  147.     }
  148.    
  149.     if (!(strcmp(pstrBuffer,"exit")))
  150.     {
  151.       logs("[iCheckInput]Client choose: exit.\n");
  152.       return SOCKET_INPUT_EXIT;
  153.     }
  154.    
  155.     if (!(strcmp(pstrBuffer,"count")))
  156.     {
  157.       logs("[iCheckInput]Client choose: count.\n");
  158.       return SOCKET_INPUT_COUNT;
  159.     }
  160.    
  161.     if (!(strcmp(pstrBuffer,"help")))
  162.     {
  163.       logs("[iCheckInput]Client choose: help.\n");
  164.       return SOCKET_INPUT_HELP;
  165.     }
  166.   }
  167.  
  168.   logs("[iCheckInput]Client write wrong argument.\n");
  169.   return ERR_SOCKET_ARGUMENTS;
  170. }
  171. /*******************************************************************************
  172.    Function: iReceive
  173.          In:
  174.         Out:
  175.      Return: 0 on succes
  176.              1 on fail
  177. Description: receive data
  178. *******************************************************************************/
  179. int iReceive(int iSockID, char *pstrBuffer)
  180. {
  181.   if ((recv (iSockID, pstrBuffer, strlen (pstrBuffer) ,0)) == -1 )
  182.   {
  183.     logs ("error: [iReceive] receive fail.\n");
  184.     return 1;
  185.   }
  186.   logs("succes: [iReceive] receive done.\n");
  187.   return 0;
  188. }
  189. /*******************************************************************************
  190.    Function: iProcess
  191.          In:
  192.         Out:
  193.      Return:
  194. Description:
  195. *******************************************************************************/
  196. int iProcess(int iSockID)
  197. {
  198.   int iRes;
  199.   char strBuffer[MAX_SOCKET_BUF_SIZE], strDate[MAX_SOCKET_BUF_SIZE];
  200.  
  201.   //INFINITE LOOP FOR CHAT
  202.  
  203.     logs("[iProcess] prints menu to client.\n");
  204.    
  205.     //PRINTS MENU TO CLIENT
  206.     iSendToClient(iSockID,"--> MENU **************************************\n");
  207.     iSendToClient(iSockID,"--> Enter command *****************************\n");
  208.     iSendToClient(iSockID,"--> getdate <ddmmyy> **************************\n");
  209.     iSendToClient(iSockID,"--> count *************************************\n");
  210.     iSendToClient(iSockID,"--> Help: for more info about the commands ****\n");
  211.     iSendToClient(iSockID,"--> Exit: exit chat ***************************\n");
  212.     iSendToClient(iSockID,"--> Quit: exit sever **************************\n");
  213.    
  214.     bzero(strBuffer, MAX_SOCKET_BUF_SIZE);
  215.    
  216.     iReceive(iSockID,strBuffer);
  217.     logs("debug: received data: %s\n", strBuffer);
  218.    
  219.     logs("[iProcess] send for check.\n");
  220.    
  221.     iRes = iCheckInputSocket(strBuffer, strDate);
  222.    
  223.     logs("[iProcess] check result: %d\n",iRes);
  224.    
  225.     if (iRes == ERR_SOCKET_ARGUMENTS_NUM)
  226.     {
  227.       logs("error: [iReceive] wrong number of arguments.\n");
  228.       iSendToClient(iSockID,"--> More than two arguments input.\n");
  229.     }
  230.    
  231.     if (iRes == ERR_SOCKET_ARGUMENTS)
  232.     {
  233.       logs("error: [iProcess] wrong arguments.\n");
  234.       iSendToClient(iSockID,"--> Wrong arguments input.\n");
  235.     }
  236.    
  237.     if (iRes == SOCKET_INPUT_QUIT)
  238.     {
  239.       logs("succes: [iReceive] server quit code.\n");
  240.       iSendToClient(iSockID,"--> Closing server..\n");
  241.       return SOCKET_INPUT_QUIT;
  242.     }
  243.     if (iRes == SOCKET_INPUT_EXIT)
  244.     {
  245.       logs("succes: [iReceive] chat exit code.\n");
  246.       iSendToClient(iSockID,"--> End of chat.\n");
  247.       //break;
  248.     }
  249.     if (iRes == SOCKET_INPUT_HELP)
  250.     {
  251.       logs("succes: [iReceive] help code.\n");
  252.       iSendToClient(iSockID,"--> Help menu loading.\n");
  253.       HelpMenu(iSockID);
  254.     }
  255.     if (iRes == SOCKET_INPUT_COUNT)
  256.     {
  257.       logs("succes: [iReceive] count type code.\n");
  258.       iSendToClient(iSockID,"--> Sending to count function.\n");
  259.       iCount(iSockID);
  260.     }
  261.     if (iRes == SOCKET_INPUT_GETDATE)
  262.     {
  263.       logs("succes: [iReceive] getdate code.\n");
  264.       iSendToClient(iSockID,"--> Sending to getdate function.\n");
  265.       getDate(iSockID,strBuffer);
  266.     }
  267.   return 0;
  268. }
  269. /*******************************************************************************
  270.    Function: iServer
  271.          In:
  272.         Out:
  273.      Return: 0 on succes
  274.              1 on fail
  275. Description:
  276. ********************************************************************************/
  277. int iServer(char *pstrValue)
  278. {
  279.   int iSockId, iConnId, iLen, iPort, iPro;
  280.   struct sockaddr_in servAddr, cliAdr;
  281.  
  282.   if(!(iPort = atoi(pstrValue)))
  283.   {
  284.     logs("error: [iServer] wrong input port.\n");
  285.     return 1;
  286.   }
  287.  
  288.   // socket create and verification
  289.   iSockId = socket(AF_INET, SOCK_STREAM, 0);
  290.   if (iSockId == -1)
  291.   {
  292.     logs("error: [iServer] socket not created.\n");
  293.     return 1;
  294.   }
  295.   logs("succes: [iServer] socket created.\n");
  296.  
  297.   bzero(&servAddr, sizeof(servAddr));
  298.  
  299.   // assign IP, PORT
  300.   servAddr.sin_family = AF_INET;
  301.   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  302.   servAddr.sin_port = htons(iPort);
  303.  
  304.   // Binding newly created socket to given IP and verification
  305.   if ((bind(iSockId, (struct sockaddr*)&servAddr, sizeof(servAddr))) != 0)
  306.   {
  307.     logs("error: [iServer] socked bind fail.\n");
  308.     return 1;
  309.   }
  310.   logs("succes: [iServer] socked bind.\n");
  311.    
  312.   // Now server is ready to listen and verification
  313.   if ((listen(iSockId, 1)) != 0)
  314.   {
  315.     logs("error: [iServer] Listen failed.\n");
  316.     return 1;
  317.   }
  318.   logs("succes: [iServer] Server listening.\n");
  319.  
  320.     iLen = sizeof(cliAdr);
  321.    
  322.     // Accept the data packet from client and verification
  323.     iConnId = accept(iSockId, (struct sockaddr*)&cliAdr, &iLen);
  324.     if (iConnId < 0)
  325.     {
  326.       logs("error: [iServer] server acccept failed.\n");
  327.       return 1;
  328.     }
  329.     logs("succes: [iServer] server acccept the client socket: %d, port: %d.\n",iConnId,iPort);
  330.    
  331.     iPro = iProcess(iConnId);
  332.    
  333.     // Function for chatting between client and server
  334.     if (iPro == SOCKET_INPUT_QUIT)
  335.     {
  336.       logs("Server quit.\n");
  337.      // break;
  338.     }
  339.  
  340.   // Close the socket
  341.   if (!(close(iSockId)))
  342.   {
  343.     logs("error: [iServer] socket close fail.\n");
  344.     return 1;
  345.   }
  346.  
  347.   logs("succes: [iServer] socket closed.\n");
  348.   return 0;
  349. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top