Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "client_socket.h"
- using std::cout;
- using std::cerr;
- using std::endl;
- using std::string;
- void *downloadFile(void *args){
- ClientSocket dataSocket('T', 7071);
- char file_name_buf[256];
- int client_fd;
- string *ar = (string *) args;
- char args_local[1<<10];
- strcpy(args_local, ((*ar) + " 1").c_str()); //1 for download, 0 for upload
- sscanf(ar->c_str(), "%d %s", &client_fd, file_name_buf);
- delete(ar);
- cerr << "Requesting file: " << file_name_buf << endl;
- dataSocket.writeToSocket(args_local);
- FILE *fout = fopen(file_name_buf, "w");
- int bufSz=1<<20; //this MUST BE >= buffer size of the FTP server, so as not to cause buffer over flow, and drop data
- char packet[bufSz];
- memset(packet,0,bufSz);
- int n, total=0;
- while((n = dataSocket.readFromSocket(packet, bufSz))){
- total+=n;
- fwrite(packet, 1, n, fout);
- }
- fclose(fout);
- cerr << "total = " << 1.0*total/1000.0 << "Kbyte" << endl;
- cerr << "File successfully received, thank God :)" << endl;
- return NULL;
- }
- bool download_file_wrapper(ClientSocket *clientSocket, string fileName){
- char file_name_buf[1<<8];
- strcpy(file_name_buf, fileName.c_str());
- cerr << "Requesting file: " << file_name_buf << endl;
- clientSocket->writeToSocket(file_name_buf);
- //========================================================
- char response[1<<8];
- clientSocket->readFromSocket(response, 1<<8);
- int client_fd;
- sscanf(response, "%d", &client_fd);
- if(client_fd < 0){
- if(client_fd==-1){
- cerr << "You have to wait until you current file transfer is finished" << endl;
- } else if(client_fd==-2){
- cerr << "Error! file not found" << endl;
- } else if(client_fd < -2){
- cerr << "Unexpected error has occurred, please try again later!" << endl;
- }
- return false;
- }
- string *arg = new string(string(response)+ " " + fileName);
- pthread_t thrd;
- pthread_create(&thrd, NULL, downloadFile, (void *) arg);
- return true;
- }
- bool uploadFile(){
- ClientSocket clientSocket('T', 7070);
- char file_name_buf[256] = "tureky.jpg";
- cerr << "Uploading file: " << file_name_buf << endl;
- clientSocket.writeToSocket(file_name_buf);
- FILE *fin = fopen(file_name_buf, "r");
- int bufSz=1<<20; //this MUST BE >= buffer size of the FTP server, so as not to cause buffer over flow, and drop data
- char packet[bufSz];
- memset(packet,0,bufSz);
- int n;
- if(!fin){
- cerr << "Error! couldn't open the file: " << file_name_buf << endl;
- // close(fd);
- return false;
- }
- while((n=fread(packet, 1, bufSz, fin))){
- clientSocket.writeToSocket(packet, n);
- }
- cerr << "closing file " << file_name_buf << endl;
- if(fclose(fin)==EOF){
- cerr << "Error! couldn't close the file: " << file_name_buf << endl;
- return false;
- }
- return true;
- }
- void upload_file_wrapper(ClientSocket *clientSocket, string fileName){
- }
- void go_ftp_client(){
- ClientSocket clientSocket('T', 7070);
- char input[1<<10];
- bool quit=false;
- while(!quit){
- printf("Select Action Number:\n---------------------\n1. Download File\n2. Upload File\n3. Quit\n");
- scanf("%s", input);
- int option;
- sscanf(input, "%d", &option);
- switch(option){
- case 1:
- printf("Enter file name:\n");
- scanf("%s", input);
- download_file_wrapper(&clientSocket, string(input));
- break;
- case 2:
- printf("Enter file name:\n");
- scanf("%s", input);
- upload_file_wrapper(&clientSocket, string(input));
- break;
- case 3:
- quit = true;
- break;
- case 4:
- printf("Invalid Option!");
- break;
- }
- printf("============================================\n\n");
- }
- }
- int main(){
- // downloadFile();
- // uploadFile();
- go_ftp_client();
- return 0;
- }
Add Comment
Please, Sign In to add comment