SHARE
TWEET

Untitled

a guest Jun 19th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. client_0: 4
  2. client_1: 8
  3. client_2: 5
  4. client_4: 7
  5.      
  6. #define PORT 60000
  7.  
  8. #include <unistd.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <vector>
  12. #include <string>
  13. #include <iostream>
  14. #include <mutex>
  15.  
  16. using namespace std;
  17.  
  18. vector<int> inputQueue; //<--------!
  19. mutex mtx; //<---------------------!
  20.  
  21. void printVector(vector<int> input) {
  22.     cout << "inputQueue: [";
  23.     for (unsigned int i = 0; i < input.size(); i++ ) {
  24.         if (i != input.size() - 1)
  25.             cout << input[i] << ", ";
  26.         else
  27.             cout << input[i];
  28.     }
  29.     cout << "]." << endl;
  30. }
  31.  
  32. int main(int argc, char const *argv[])
  33. {
  34.     int server_fd, client_fd;
  35.     struct sockaddr_in address;
  36.     int opt = 1;
  37.     int addrlen = sizeof(address);
  38.  
  39.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
  40.         perror("socket failed");
  41.         exit(EXIT_FAILURE);
  42.     }
  43.     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
  44.         perror("setsockopt");
  45.         exit(EXIT_FAILURE);
  46.     }
  47.     address.sin_family = AF_INET;
  48.     address.sin_addr.s_addr = INADDR_ANY;
  49.     address.sin_port = htons( PORT );
  50.     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
  51.         perror("bind failed");
  52.         exit(EXIT_FAILURE);
  53.     }
  54.     if (listen(server_fd, 10) < 0) {
  55.         perror("listen");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.     while(1) {
  59.         char buffer[4];
  60.         if ((client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
  61.             perror("accept");
  62.             exit(EXIT_FAILURE);
  63.         }
  64.         if (!fork()) {
  65.             recv(client_fd, buffer, 4, MSG_WAITALL);
  66.             int receivedInt = int(
  67.                     (unsigned char)(buffer[0]) << 24 |
  68.                     (unsigned char)(buffer[1]) << 16 |
  69.                     (unsigned char)(buffer[2]) << 8 |
  70.                     (unsigned char)(buffer[3])
  71.             );
  72.             mtx.lock(); //<-------------------------------------!
  73.             inputQueue.push_back(receivedInt); //<--------------!
  74.             cout << "Client context. Integer registered: " << receivedInt << ": inputQueue length is " << inputQueue.size() << endl;
  75.             printVector(inputQueue); //<------------------------!
  76.             mtx.unlock(); //<-----------------------------------!
  77.             close(server_fd); close(client_fd);
  78.         }
  79.         cout << "Server context: inputQueue length is " << inputQueue.size() << endl;
  80.         printVector(inputQueue);
  81.     }
  82.     return 0;
  83. }
  84.      
  85. Server context: inputQueue length is 0
  86. inputQueue: [].
  87. Client context. Integer registered: 4: inputQueue length is 1
  88. inputQueue: [4].
  89. Server context: inputQueue length is 1
  90. inputQueue: [4].
  91. Server context: inputQueue length is 0
  92. inputQueue: [].
  93. Client context. Integer registered: 8: inputQueue length is 1
  94. inputQueue: [8].
  95. Server context: inputQueue length is 0
  96. inputQueue: [].
  97. Server context: inputQueue length is 1
  98. inputQueue: [8].
  99. Client context. Integer registered: 5: inputQueue length is 1
  100. inputQueue: [5].
  101. Server context: inputQueue length is 1
  102. inputQueue: [5].
  103. Server context: inputQueue length is 0
  104. inputQueue: [].
  105. Client context. Integer registered: 7: inputQueue length is 1
  106. inputQueue: [7].
  107. Server context: inputQueue length is 1
  108. inputQueue: [7].
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