SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Protocol Agreement: Lab12-RFC.pdf
  2. Description:
  3.     Receives message M from the client and extracts a (operand 1), b (operand 2), and c (opcode).
  4.     Performs the operation requested and sends back the result with repeating the operands and operation.
  5.     The size of the message M (at the application layer) should not exceed 16 bytes.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13.  
  14. #define REQUEST_LENGTH 9
  15. #define RESPONSE_LENGTH 14
  16.  
  17. /* Global Variables */
  18. char requestMessage[9], responseMessage[14], operationCode, isAnswerValid;
  19. unsigned int operandA, operandB, answer;
  20.  
  21. void updatePacket() {
  22.     memcpy(responseMessage + sizeof(char) + sizeof(unsigned int) + sizeof(unsigned int), &answer, sizeof(unsigned int));
  23.     memcpy(responseMessage + sizeof(char) + sizeof(unsigned int) + sizeof(unsigned int) + sizeof(unsigned int), &isAnswerValid, sizeof(char));
  24. }
  25.  
  26. void deconstructPacket() {
  27.     memcpy(&operationCode, requestMessage, sizeof(char));
  28.     memcpy(&operandA, requestMessage + sizeof(char), sizeof(unsigned int));
  29.     memcpy(&operandB, requestMessage + sizeof(char) + sizeof(unsigned int), sizeof(unsigned int));
  30. }
  31.  
  32. void calculateAnswer() {
  33.     if (operandB == 0 && operationCode == '/') {
  34.         isAnswerValid = '2';
  35.         answer = 0;
  36.     }
  37.     else {
  38.         isAnswerValid = '1';
  39.         switch(operationCode) {
  40.         case '+':
  41.             answer = operandA  + operandB;
  42.             break;
  43.         case '-':
  44.             answer = operandA - operandB;
  45.             break;
  46.         case 'x':
  47.             answer = operandA * operandB;
  48.             break;
  49.         case '/':
  50.             answer = operandA / operandB;
  51.             break;
  52.         }
  53.     }
  54. }
  55.  
  56. int main(int argc, char *argv[]) {
  57.     int listenSocket, dataSocket, port, n;
  58.     struct sockaddr_in serverSocketAddress, clientSocketAddress;
  59.     socklen_t clientLength;
  60.     pid_t pid;
  61.  
  62.     /* Argument check */
  63.     if (argc < 2) {
  64.         perror("Usage: ./server12 'Port Number'");
  65.         exit(1);
  66.     }
  67.  
  68.     /* Create the socket */
  69.     if ((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  70.         perror("Problem creating the socket");
  71.         exit(2);
  72.     }
  73.     memset(&serverSocketAddress, 0, sizeof(serverSocketAddress));
  74.     memset(&clientSocketAddress, 0, sizeof(clientSocketAddress));
  75.     port = atoi(argv[1]);
  76.     serverSocketAddress.sin_family = AF_INET;
  77.     serverSocketAddress.sin_addr.s_addr = INADDR_ANY;
  78.     serverSocketAddress.sin_port = htons(port);
  79.  
  80.     /* Bind the listenSocket to the server & begin listening; set maximum number of connected hosts */
  81.     if (bind(listenSocket, (struct sockaddr *) &serverSocketAddress , sizeof(serverSocketAddress)) < 0) {
  82.         perror("Problem binding the socket");
  83.         exit(3);
  84.     }
  85.     listen(listenSocket, 5);
  86.    
  87.     printf("%s\n", "Server running... waiting for connections.");
  88.  
  89.     for ( ; ; ) {
  90.        
  91.         /* Accept an incoming client connection */
  92.         clientLength = sizeof(clientSocketAddress);
  93.         dataSocket = accept(listenSocket, (struct sockaddr *) &clientSocketAddress, &clientLength);
  94.        
  95.         /* Concurrent connection handler */
  96.         if ((pid = fork()) == 0) {
  97.             close(listenSocket);
  98.            
  99.             /* Recieve the operation request */
  100.             n = recv(dataSocket, requestMessage, REQUEST_LENGTH + 1, MSG_WAITALL);
  101.             deconstructPacket();
  102.            
  103.             printf("\n%s", "operandA received from client: ");
  104.             printf("%d", operandA);
  105.             printf("\n%s", "operandB received from client: ");
  106.             printf("%d", operandB);
  107.             printf("\n%s", "operationCode received from client: ");
  108.             printf("%c", operationCode);
  109.            
  110.             calculateAnswer();
  111.             printf("\n%s", "The answer to the requested operation: ");
  112.             printf("%d\n\n", answer);
  113.            
  114.             /* Add the answer & isAnswerValid values to the responseMessage */
  115.             updatePacket();
  116.            
  117.             /* Send the responseMessage to the client */
  118.             if (n = send(dataSocket, responseMessage, RESPONSE_LENGTH + 2, 0) < 0) {
  119.                 perror("Problem adding the packet to the buffer");
  120.                 exit(4);
  121.             }
  122.            
  123.             /* Close a connected data socket; remember - server is still listening */
  124.             close(dataSocket);
  125.             exit(5);
  126.         }
  127.         close(dataSocket);
  128.     }
  129. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top