void dir(){
	// send command //
	allzero(buffer, 1);
	send_pack(DIRL, buffer, sock);
		
	/* send port number for data connection to server */
	send_data_port();
	
	// make socket and accept server's Data Connection (active)//
	makeDataConnection();
	fprintf(stderr, "get() : data connection accepted\n");
	
	// recieve file list //
	receive_file_list();
	fprintf(stdout, "get() : complete file received\n");

	// close the data connection //
	close(request->serverSocket);
}
Beispiel #2
0
//-----------------------------------------------------------------------------
void *connection(void *user) {
  //struct timeval curTime;
  struct User *_user = (struct User *)user;
  int e;
  int rc = 1;
  int sockfd = _user->sockfd;
  pthread_detach(pthread_self()); //automatically clears the threads memory on exit
  fd_set rdset, wrset;
  int s = -1;
  int max_fd = sockfd + 1;
  struct timeval selectTimeout;
  
  selectTimeout.tv_sec = TIMEOUT / 1000;
  selectTimeout.tv_usec = (TIMEOUT % 1000) * 1000;
  FD_ZERO(&rdset);
  FD_ZERO(&wrset);
  
  char *buf = calloc(100,1);
  for (;;) {
    // start monitoring for reads or timeout
    if (s <= 0) {
      FD_SET(sockfd, &rdset);
    }

    s = select(max_fd, &rdset, &wrset, NULL, &selectTimeout); 

    if (s == -1) { printf("ERROR: Socket error. Exiting.\n"); exit(1); }

    /*
     * Here we handle all of the incoming text from a particular client.
     */
    if(s > 0) {
      bzero(buf,100);
      rc = recv(sockfd,buf,99,0);
      if (rc > 0)
      {
        char *cmd = malloc(24);
        cmd = strtok(buf," ");
 
        // which command are we processing? 
        if (strcmp(cmd,GET_CMD) == 0) {
          char *filename = calloc(FILENAME_LEN,1);
          filename = strtok(NULL,"\0");
          send_file_info(sockfd, filename);
        }
        else if (strcmp(cmd,LIST_CMD) == 0) {
          send_file_list(sockfd);
        }
        else if (strcmp(cmd,USERS_CMD) == 0) {
          send_user_list(sockfd);
        }
        else if (strcmp(cmd,SEND_LIST_CMD) == 0) {
          receive_file_list(sockfd,_user);
        }
        else if (strcmp(cmd,EXIT_CMD) == 0) {
          //if I received an 'exit' message from this client
	        pthread_mutex_lock(&mutex);
	        remove_user(_user);
	        pthread_mutex_unlock(&mutex);
	        pthread_exit(NULL);
	        printf("Shouldn't see this!\n");
          //loop through global client list and
	        //e =write(..); 
	        if (e == -1) //broken pipe.. someone has left the room
	        {
	          pthread_mutex_lock(&mutex);
	          //so remove them from our list
	          pthread_mutex_unlock(&mutex);
	        }
        }
        else {
          printf("Unknown command received: %s\n",cmd);
        }
        fflush(stdout);
      }
    }
  }	
	//A requirement for 5273 students:
  //if I received a new files list from this client, the
	//server must “Push”/send the new updated hash table to all clients it is connected to.

  //should probably never get here
  return 0;
}