SHARE
TWEET

Untitled

a guest Mar 26th, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <pthread.h>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. std::vector<pthread_t> threads;
  14. std::vector<int> clients;
  15. int serverSd;
  16.  
  17. void* connectToClient(void* serverSd);
  18.  
  19. void waitForConnection() {
  20.     while(true) {
  21.         sockaddr_in newSockAddr;
  22.         socklen_t newSockAddrSize = sizeof(newSockAddr);
  23.         int newSd = accept(*(int*)serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize);
  24.         if(newSd < 0)
  25.         {
  26.             cerr << "Error accepting request from client!" << endl;
  27.             close(newSd);
  28.             continue;
  29.         }
  30.         pthread_t tempThead;
  31.         threads.push_back(tempThead);
  32.         clients.push_back(newSd);
  33.         pthread_create(&tempThead,NULL,&connectToClient,(void*)newSd);
  34.     }
  35. }
  36.  
  37. void* connectToClient(void* newSd) {
  38.     int currentPosition = clients.size() - 1;
  39.     char msg[250];
  40.     cout << "Connected with client!" << endl;
  41.     while(1) {
  42.         cout << "Awaiting client response..." << endl;
  43.         memset(&msg, 0, sizeof(msg));//clear the buffer
  44.         recv(*(int *) newSd, (char *) &msg, sizeof(msg), 0);
  45.         if (!strcmp(msg, "exit")) {
  46.             cout << "Client has quit the session" << endl;
  47.             break;
  48.         }
  49.         for (int i = 0; i < clients.size(); i++) {
  50.             send(clients[i], (char *) &msg, strlen(msg), 0);
  51.         }
  52.     }
  53.     clients.erase(clients.begin() + currentPosition);
  54.     close(*(int*)newSd);
  55. }
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59.     if(argc != 2)
  60.     {
  61.         cerr << "Usage: port" << endl;
  62.         exit(0);
  63.     }
  64.     int port = atoi(argv[1]);
  65.  
  66.     sockaddr_in servAddr;
  67.     bzero((char*)&servAddr, sizeof(servAddr));
  68.     servAddr.sin_family = AF_INET;
  69.     servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  70.     servAddr.sin_port = htons(port);
  71.  
  72.     serverSd = socket(AF_INET, SOCK_STREAM, 0);
  73.     if(serverSd < 0)
  74.     {
  75.         cerr << "Error establishing the server socket" << endl;
  76.         exit(0);
  77.     }
  78.     int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr,
  79.                           sizeof(servAddr));
  80.     if(bindStatus < 0)
  81.     {
  82.         cerr << "Error binding socket to local address" << endl;
  83.         exit(0);
  84.     }
  85.     cout << "Waiting for a client to connect..." << endl;
  86.     listen(serverSd, 5);
  87.     waitForConnection();
  88.     close(serverSd);
  89.     cout << "Connection closed..." << endl;
  90.     return 0;
  91. }
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