/*
 * ftpc program sends the target file
 * to the server specified by the user
 * with ip address and port number.
 */
int main(int argc, char *argv[]) {
	int sockfd = -1;

	// precheck arguments
	if(preCheckArgs(argc, argv) == -1) {
		exit(-1);
	}

	// create the socket
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0) {
		fprintf(stderr, "ftpc: ERROR: Can't create the socket.\n");
		exit(-1);
	}

	// connect to the server
	if(connectServer(sockfd, /*ip address*/argv[1], /*port*/argv[2]) < 0) {
		exit(-1);
	}

	// send the file to the server
	sendFileToServer(sockfd, argv[3]);

	return 0;
}
Exemple #2
0
int clientInput(int sock){

	int choice = -1;
	char path[100], fileName[100];
	char keywords[100];
	int c;

	// Print Menu for selecting Indexing or Search Query
	printf("Select Job:\n1. Indexing\n2. Search\n3. Exit\nChoice: ");

	char line[2];
	if (fgets(line, sizeof(line), stdin)) {
	    sscanf(line, "%d", &choice);
	}

	switch(choice){
	case 1:
	{
		// Indexing

		// Re-initialize choice
		choice = 0;

		//Step 1 : Inform the server of the type of request
		if(sendInt(sock, 1) < 0){
			puts("Indexing Request Failed : Send request type failed");
			return -1;
		}

		//Step 2 : Ask user for input directory with the files to index
		//flushing input stream
		while((c = getchar()) != '\n' && c != EOF);

		puts("Enter the directory path with the files you want to index : ");
		fgets(path, 100, stdin);

		struct stat s;

		int index;
		for(index = 0; index < 100; index++){
			if(path[index] == '\n'){
				path[index] = '\0';
				break;
			}
		}

		printf("Path is = %s\n",path);

		if (stat(path, &s) == 0){
			if(S_ISDIR(s.st_mode)){
				puts("Directory");

				// Check directory for files, we only send .txt files
				DIR *dp;
				struct dirent *ep;
				dp = opendir (path);

				if (dp != NULL){
					while ((ep = readdir (dp)) != NULL){
						strcpy(fileName, ep->d_name);

						// If it is a .txt file, then send it over
						if(strstr(fileName, ".txt")){
							// Inform the server if there is a file to send
							if(sendInt(sock, 1) < 0){
								puts("Sending File present failed");
								break;
							}

							if(sendFileToServer(sock, path, fileName) < 0){
								printf("%s : Send Failed\n",fileName);
								break;
							}else{
								printf("%s : Sent\n",fileName);
							}

							// Wait for status of server after file sent
							if(readInt(sock, &choice) < 0){
								puts("Waiting for status of file response DIR failed");
							}

							if(choice < 0){
								puts("Indexing Failure");
								break;
							}
						}
					}

					if(choice >= 0){
						// Inform the server there are no more files to send
						if(sendInt(sock, -1) < 0){
							puts("Sending File not present failed");
							return -1;
						}
					}

					(void) closedir (dp);
				}else{
					perror ("Couldn't open the directory");
					// Inform the server there was error in opening directory
					if(sendInt(sock, -2) < 0){
						puts("Sending error in opening directory failed");
						return -1;
					}
				}

			}else{
				if(S_ISREG(s.st_mode)){
					puts("Regular File");

					// Inform the server if there is a file to send
					if(sendInt(sock, 1) < 0){
						puts("Sending File present failed");
						break;
					}

					// Extracting file and directory from path
					char *bname = basename(path);
					char *dname = dirname(path);

					strcpy(fileName, bname);
					strcpy(path, dname);

					printf("FileName = %s, DirName = %s\n",fileName, path);

					if(sendFileToServer(sock, path, fileName) < 0){
						printf("%s : Send Failed\n",fileName);
						break;
					}else{
						printf("%s : Sent\n",fileName);
					}

					// Wait for status of server after file sent
					if(readInt(sock, &choice) < 0){
						puts("Waiting for status of file response REG failed");
					}

					if(choice  < 0){
						puts("Indexing Failure | Try Again");
					}else{
						puts("Indexing Success");
						// Inform the server there are no more files to send
						if(sendInt(sock, -1) < 0){
							puts("Sending File not present failed");
							return -1;
						}
					}
				}else{
					puts("Invalid Path");

					// Inform the server that the REG file path was invalid
					if(sendInt(sock, -2) < 0){
						puts("Sending Invalid Path :  failed");
						return -1;
					}
				}
			}
		}else{
			puts("Failed to determine directory or file");
			// Inform the server there was a error in the file path
			if(sendInt(sock, -2) < 0){
				puts("Sending Failed to determine directory or file :  failed");
				return -1;
			}
		}
		break;
	}
	case 2:
	{
		//Step 1 : Inform the server of the type of request
		if(sendInt(sock, 2) < 0){
			puts("Search Request Failed : Send request type failed");
			return -1;
		}

		// Search
		puts("Enter the keywords for searching:");

		//flushing the input stream
		while((c = getchar()) != '\n' && c != EOF);

		fgets(keywords, 100, stdin);

		//Remove the \n char for the keyword string
		int index;
		for(index = 0; index < 100; index++){
			if(keywords[index] == '\n'){
				keywords[index] = '\0';
				break;
			}
		}

		// Send the string across
		if(sendString(sock, 100, &keywords[0]) < 0){
			puts("Sending keywords failed");
			return -1;
		}

		// Wait for result
		if(waitForSearchResult(sock) < 0){
			puts("Search Result from server failed.");
			return -1;
		}

		break;
	}
	case 3:
		//Exit
		//Let the server know this is an exit request
		if(sendInt(sock, 3) < 0){
			puts("Exit Request Failed");
		}
		return -1;
	default:
		// Invalid Option, wait for a valid response
		puts("Invalid Option");

		//flushing input stream
		while((c = getchar()) != '\n' && c != EOF);

		return 0;
	}

	// Operation Complete, go back to menu
	return 1;
}