fileBuffer sizeofchar bytesRemain else bytesRead readsocketID fileBuffer

Filebuffer sizeofchar bytesremain else bytesread

This preview shows page 22 - 26 out of 36 pages.

fileBuffer, (sizeof(char) * bytesRemain)); } else { bytesRead = read(socketID, fileBuffer, (sizeof(char) * NET_BUF_SIZE)); } write(openFile, fileBuffer, bytesRead); bytesRemain = bytesRemain - bytesRead; } } else if (tempChar == '1') // File not found { printf("File not found\n"); } else if (tempChar == '2') // Denied access { printf("Access denied\n"); } else if (tempChar == '3') // Other error { printf("Could not get file\n"); } else { //Shouldn't get here printf("Something went wrong...\n"); } // Close the opened file close(openFile); } else if (strcmp("put", tempToken) == 0) { 22
Image of page 22
// put command stuff here // Set opcode in tempChar tempChar = 'D'; // Get the filename tempToken = strtok(NULL, TOK_DELIM_NS); if(tempToken == NULL) { printf("No file name specified\n"); continue; } int2bytes = strlen(tempToken) + 1; int2bytes = htons(int2bytes); // Open file for sending openFile = open(tempToken, O_RDONLY); if (openFile == -1) { if (errno == ENOENT) { printf("Could not find file " "to send\n"); } else if (errno == EACCES) { printf("Permission denied\n"); } else { printf("Could not open file " "to send\n"); } continue; } // Send opcode write(socketID, &tempChar, sizeof(tempChar)); // Send filename length write(socketID, &int2bytes, sizeof(int2bytes)); // Send filename write(socketID, tempToken, (sizeof(char) * ntohs(int2bytes))); // Get back opcode read(socketID, &tempChar, sizeof(tempChar)); // Get back status code read(socketID, &tempChar, sizeof(tempChar)); if (tempChar == '0') // Can send file { // Set next opcode and send tempChar = 'E'; write(socketID, &tempChar, sizeof(tempChar)); // Get file info stat(tempToken, &fileInfo); int4bytes = fileInfo.st_size; // Send file size int4bytes = htonl(int4bytes); 23
Image of page 23
write(socketID, &int4bytes, sizeof(int4bytes)); int4bytes = ntohl(int4bytes); bytesRemain = int4bytes; // Loop through and send file while (bytesRemain) { if (bytesRemain < NET_BUF_SIZE) { bytesRead = read(openFile, fileBuffer, (sizeof(char) * bytesRemain)); } else { bytesRead = read(openFile, fileBuffer, (sizeof(char) * NET_BUF_SIZE)); } write(socketID, fileBuffer, bytesRead); bytesRemain = bytesRemain - bytesRead; } // Close the open file close(openFile); } else if (tempChar == '1') // Filename clash { printf("A file of that name already " "exists on the server\n"); } else if (tempChar == '2') // Create error { printf("Server could not create file " "for saving\n"); } else if (tempChar == '3') // Other error { printf("The server incountered an " "unspecified error\n"); } else // Just in case { printf("Something went wrong...\n"); } } else if (strcmp("pwd", tempToken) == 0) { // pwd command stuff here // Set opcode in tempChar tempChar = 'E'; 24
Image of page 24
// Send opcode write(socketID, &tempChar, sizeof(tempChar)); // Get back opcode response read(socketID, &tempChar, sizeof(tempChar)); // Get back length of pwd read(socketID, &int2bytes, sizeof(int2bytes)); // Get pwd read(socketID, tempString, (sizeof(char) * ntohs(int2bytes))); // Print out pwd printf("%s\n", tempString); } else if (strcmp("lcd", tempToken) == 0) { // lcd command stuff here tempToken = strtok(NULL, TOK_DELIM); check = chdir(tempToken); if (check == -1) // If dir change fails { if (errno == ENOENT) { printf("Directory does not " "exist\n"); } else if (errno == EACCES) { printf("Permission denied\n"); } else { printf("Unspecified error. " "Directory not " "changed\n"); } } } else if (strcmp("ldir", tempToken) == 0) { // ldir stuff here numFiles = scandir(".", &fileList, NULL, alphasort); if (numFiles == 0) { perror("myftp:scandir"); } else { for (i = 0; i < numFiles; ++i) { if (fileList[i]->d_type == DT_REG) { printf("f "); } else if (fileList[i]->d_type == DT_DIR) { printf("d "); 25
Image of page 25
Image of page 26

You've reached the end of your free preview.

Want to read all 36 pages?

  • One '14

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture