Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1.   for( ;; ) {
  2.     /* Accept a connection and then create a child to do the work */
  3.     /* loop back and wait for another connection                  */
  4.  
  5.     printf( "server: starting accept\n");
  6.     if ((newsd = accept(sockid ,(struct sockaddr *) &client_addr, &clilen)) < 0) {
  7.       perror( "Failed to accept" );
  8.       exit(0);
  9.     }
  10.     printf("server: return from accept, socket for this ftp: %d\n ", newsd);
  11.     if ((readn (newsd,(char*)command,5)) < 0) {
  12.       perror( "Read error" );
  13.       exit(0);
  14.     }
  15.     std::string cmd = (char*)command;
  16.     if (cmd == "join") {
  17.       printf( "server: identified command process\n");
  18.       pthread_create(&tID[num_threads], NULL, commandThread, (void*)newsd);
  19.       printf( "server: command thread created\n");
  20.       num_threads++;
  21.     }
  22.     else if (cmd == "read") {
  23.       printf( "server: identified reader process\n");
  24.       char* cID[1024];
  25.       int cID_len, validcID = 0;
  26.       std::string clientID;
  27.       if (readn (newsd,(char*)&cID_len, sizeof(cID_len)) < 0) {
  28.         perror( "Read error" );
  29.         exit(0);
  30.       }
  31.       if (readn (newsd,(char*)cID, cID_len) < 0) {
  32.         perror( "Read error" );
  33.         exit(0);
  34.       }
  35.       clientID = (char*)cID;
  36.       Client* readClient = clientCollection.findClientByID(clientID);
  37.       if (readClient != NULL) {
  38.         validcID = 1;
  39.         writen(newsd, (char*)&validcID, sizeof(validcID));
  40.         pthread_create(&tID[num_threads], NULL, readerThread, (void*)readClient);
  41.         num_threads++;
  42.       }
  43.       else writen(newsd, (char*)&validcID, sizeof(validcID));
  44.     }
  45.  
  46.     }
  47.     else if (cmd == "writ") {
  48.       printf( "server: identified writer process\n");
  49.     }
  50.     else printf("server: unrecognized process\n");
  51.     /* Parent continues below here */
  52.     // close(newsd);    /* parent all done with client, only child */
  53.   }                  /* will communicate with that client from now on */
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement