Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for( ;; ) {
- /* Accept a connection and then create a child to do the work */
- /* loop back and wait for another connection */
- printf( "server: starting accept\n");
- if ((newsd = accept(sockid ,(struct sockaddr *) &client_addr, &clilen)) < 0) {
- perror( "Failed to accept" );
- exit(0);
- }
- printf("server: return from accept, socket for this ftp: %d\n ", newsd);
- if ((readn (newsd,(char*)command,5)) < 0) {
- perror( "Read error" );
- exit(0);
- }
- std::string cmd = (char*)command;
- if (cmd == "join") {
- printf( "server: identified command process\n");
- pthread_create(&tID[num_threads], NULL, commandThread, (void*)newsd);
- printf( "server: command thread created\n");
- num_threads++;
- }
- else if (cmd == "read") {
- printf( "server: identified reader process\n");
- char* cID[1024];
- int cID_len, validcID = 0;
- std::string clientID;
- if (readn (newsd,(char*)&cID_len, sizeof(cID_len)) < 0) {
- perror( "Read error" );
- exit(0);
- }
- if (readn (newsd,(char*)cID, cID_len) < 0) {
- perror( "Read error" );
- exit(0);
- }
- clientID = (char*)cID;
- Client* readClient = clientCollection.findClientByID(clientID);
- if (readClient != NULL) {
- validcID = 1;
- writen(newsd, (char*)&validcID, sizeof(validcID));
- pthread_create(&tID[num_threads], NULL, readerThread, (void*)readClient);
- num_threads++;
- }
- else writen(newsd, (char*)&validcID, sizeof(validcID));
- }
- }
- else if (cmd == "writ") {
- printf( "server: identified writer process\n");
- }
- else printf("server: unrecognized process\n");
- /* Parent continues below here */
- // close(newsd); /* parent all done with client, only child */
- } /* will communicate with that client from now on */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement