Guest User

Untitled

a guest
Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "client_socket.h"
  4.  
  5. using std::cout;
  6. using std::cerr;
  7. using std::endl;
  8. using std::string;
  9.  
  10. void *downloadFile(void *args){
  11.         ClientSocket dataSocket('T', 7071);
  12.  
  13.         char file_name_buf[256];
  14.         int client_fd;
  15.  
  16.         string *ar = (string *) args;
  17.         char args_local[1<<10];
  18.         strcpy(args_local, ((*ar) + " 1").c_str()); //1 for download, 0 for upload
  19.         sscanf(ar->c_str(), "%d %s", &client_fd, file_name_buf);
  20.         delete(ar);
  21.  
  22.         cerr << "Requesting file: " << file_name_buf << endl;
  23.         dataSocket.writeToSocket(args_local);
  24.  
  25.  
  26.         FILE *fout = fopen(file_name_buf, "w");
  27.  
  28.         int bufSz=1<<20; //this MUST BE >= buffer size of the FTP server, so as not to cause buffer over flow, and drop data
  29.         char packet[bufSz];
  30.         memset(packet,0,bufSz);
  31.         int n, total=0;
  32.  
  33.         while((n = dataSocket.readFromSocket(packet, bufSz))){
  34.             total+=n;
  35.             fwrite(packet, 1, n, fout);
  36.         }
  37.  
  38.         fclose(fout);
  39.  
  40.         cerr << "total = " << 1.0*total/1000.0 << "Kbyte" << endl;
  41.         cerr << "File successfully received, thank God :)" << endl;
  42.  
  43.         return NULL;
  44. }
  45.  
  46. bool download_file_wrapper(ClientSocket *clientSocket, string fileName){
  47.     char file_name_buf[1<<8];
  48.     strcpy(file_name_buf, fileName.c_str());
  49.     cerr << "Requesting file: " << file_name_buf << endl;
  50.     clientSocket->writeToSocket(file_name_buf);
  51.  
  52. //========================================================
  53.     char response[1<<8];
  54.     clientSocket->readFromSocket(response, 1<<8);
  55.     int client_fd;
  56.     sscanf(response, "%d", &client_fd);
  57.  
  58.     if(client_fd < 0){
  59.         if(client_fd==-1){
  60.             cerr << "You have to wait until you current file transfer is finished" << endl;
  61.         } else if(client_fd==-2){
  62.             cerr << "Error! file not found" << endl;
  63.         } else if(client_fd < -2){
  64.             cerr << "Unexpected error has occurred, please try again later!" << endl;
  65.         }
  66.  
  67.         return false;
  68.     }
  69.  
  70.     string *arg = new string(string(response)+ " " + fileName);
  71.     pthread_t thrd;
  72.     pthread_create(&thrd, NULL, downloadFile, (void *) arg);
  73.  
  74.     return true;
  75. }
  76.  
  77. bool uploadFile(){
  78.     ClientSocket clientSocket('T', 7070);
  79.  
  80.     char file_name_buf[256] = "tureky.jpg";
  81.     cerr << "Uploading file: " << file_name_buf << endl;
  82.     clientSocket.writeToSocket(file_name_buf);
  83.  
  84.     FILE *fin = fopen(file_name_buf, "r");
  85.  
  86.     int bufSz=1<<20; //this MUST BE >= buffer size of the FTP server, so as not to cause buffer over flow, and drop data
  87.     char packet[bufSz];
  88.     memset(packet,0,bufSz);
  89.     int n;
  90.  
  91.     if(!fin){
  92.         cerr << "Error! couldn't open the file: " << file_name_buf << endl;
  93. //      close(fd);
  94.         return false;
  95.     }
  96.  
  97.     while((n=fread(packet, 1, bufSz, fin))){
  98.         clientSocket.writeToSocket(packet, n);
  99.     }
  100.  
  101.     cerr << "closing file " << file_name_buf << endl;
  102.     if(fclose(fin)==EOF){
  103.         cerr << "Error! couldn't close the file: " << file_name_buf << endl;
  104.         return false;
  105.     }
  106.  
  107.     return true;
  108. }
  109.  
  110. void upload_file_wrapper(ClientSocket *clientSocket, string fileName){
  111.  
  112. }
  113.  
  114. void go_ftp_client(){
  115.     ClientSocket clientSocket('T', 7070);
  116.     char input[1<<10];
  117.     bool quit=false;
  118.  
  119.     while(!quit){
  120.         printf("Select Action Number:\n---------------------\n1. Download File\n2. Upload File\n3. Quit\n");
  121.         scanf("%s", input);
  122.         int option;
  123.         sscanf(input, "%d", &option);
  124.  
  125.         switch(option){
  126.             case 1:
  127.                 printf("Enter file name:\n");
  128.                 scanf("%s", input);
  129.                 download_file_wrapper(&clientSocket, string(input));
  130.                 break;
  131.             case 2:
  132.                 printf("Enter file name:\n");
  133.                 scanf("%s", input);
  134.                 upload_file_wrapper(&clientSocket, string(input));
  135.                 break;
  136.             case 3:
  137.                 quit = true;
  138.                 break;
  139.             case 4:
  140.                 printf("Invalid Option!");
  141.                 break;
  142.         }
  143.  
  144.         printf("============================================\n\n");
  145.     }
  146. }
  147.  
  148. int main(){
  149. //  downloadFile();
  150. //  uploadFile();
  151.     go_ftp_client();
  152.     return 0;
  153. }
Add Comment
Please, Sign In to add comment